The Tag
object represents different tags of HTML and XML documents. The creation of Tag
objects is done when parsing the documents. The different HTML/XML tags identified during parsing are represented as corresponding Tag
objects and these objects will have attributes and contents of the HTML/XML tag. The Tag
objects can be used for searching and navigation within the HTML/XML document.
BeautifulSoup
allows us to access any Tag
object. For example, we can access the first occurrence of the <a>
tag in the next example by simply calling the name of the tag <a>
.
html_atag = """<html><body><p>Test html a tag example</p> <a href="http://www.packtpub.com'>Home</a> <a href="http;//www.packtpub.com/books'>Books</a> </body> </html>""" soup = BeautifulSoup(html_atag,'lxml') atag = soup.a print(atag)
The previous script will print the first <a>
tag in the document. We can see...