# This program uses requests to fetch an HTML page and BeautifulSoup to parse it

import requests, bs4

# Creating a BeautifulSoup Object from HTML
res = requests.get("http://nostarch.com")
res.raise_for_status()  # Raise error if nothing fetched
noStarchSoup = bs4.BeautifulSoup(res.text, "lxml")  # Specify parser to avoid warning
print(type(noStarchSoup))

exampleFile = open("example.html")
exampleSoup = bs4.BeautifulSoup(exampleFile, "lxml")
print(type(exampleSoup))

# Finding an Element with the select() Method
exampleFile = open("example.html")
exampleSoup = bs4.BeautifulSoup(exampleFile.read(), "lxml")
elems = exampleSoup.select("#author")
print(type(elems))
print(len(elems))
print(type(elems[0]))
print(elems[0].getText())
print(str(elems[0]))
print(elems[0].attrs)

pElems = exampleSoup.select('p')
print(str(pElems[0]))
print(pElems[0].getText())
print(str(pElems[1]))
print(pElems[1].getText())
print(str(pElems[2]))
print(pElems[2].getText())

@@ -943,20 +943,276 @@
 Elems%5B2%5D.getText())%0A
+%0A# Getting Data from an Element's Attributes%0Asoup = bs4.BeautifulSoup(open(%22example.html%22), %22lxml%22)%0AspanElem = soup.select('span')%5B0%5D%0Aprint(str(spanElem))%0Aprint(spanElem.get(%22id%22))%0Aprint(spanElem.get(%22some_nonexistent_addr%22) is None)%0Aprint(spanElem.attrs)%0A

Update P02_parseHTML added element attributes example