xxxxxxxxxx
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'html.parser')
print(soup.prettify())
# <html>
# <head>
# <title>
# The Dormouse's story
# </title>
# </head>
# <body>
# <p class="title">
# <b>
# The Dormouse's story
# </b>
# </p>
# <p class="story">
# Once upon a time there were three little sisters; and their names were
# <a class="sister" href="http://example.com/elsie" id="link1">
# Elsie
# </a>
# ,
# <a class="sister" href="http://example.com/lacie" id="link2">
# Lacie
# </a>
# and
# <a class="sister" href="http://example.com/tillie" id="link3">
# Tillie
# </a>
# ; and they lived at the bottom of a well.
# </p>
# <p class="story">
# ...
# </p>
# </body>
# </html>
xxxxxxxxxx
import requests
from bs4 import BeautifulSoup
# Send request to url and save the response
response = requests.get("http://somewebpage.com....")
# Get the text out of response as html
html_doc = response.text
# Parse the text as html
soup = BeautifulSoup(html_doc, 'html.parser')
# Page text
page_content = soup.get_text() # Get the text content on that url
# Access a tag
tag_obj = soup.title # The title tag
# Child Tag inside that tag
child_tag = tag_obj.div # The first div tag inside title
# Access the parent tag
parent_tag = child_tag.parent # the title tag
# Access the sibling tag
sibling_tag = child_tag.next_sibling # the sibling tag next to the child tag
# Access the child tag attributes
child_tag_att = child_tag.attrs # the title tag attributes as dictionary
# Access the contents of child tag
child_contents = child_tag.string # var.tag_name.string
# Filter for specific items
table_rows = soup.find_all(name='tr') # Produce list of <tr> tags
xxxxxxxxxx
soup = BeautifulSoup(plateRequest.text)
#print(soup.prettify())
#print soup.find_all('tr')
table = soup.find("table", { "class" : "lineItemsTable" })
for row in table.findAll("tr"):
cells = row.findAll("td")
print cells
xxxxxxxxxx
soup.title
# <title>The Dormouse's story</title>
soup.title.name
# u'title'
soup.title.string
# u'The Dormouse's story'
soup.title.parent.name
# u'head'
soup.p
# <p class="title"><b>The Dormouse's story</b></p>
soup.p['class']
# u'title'
soup.a
# <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
soup.find_all('a')
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
# <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
soup.find(id="link3")
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>
xxxxxxxxxx
html_doc = """<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story"></p>
"""