First, Introduction
XPath is a language that looks for information in an XML document. XPath can be used to traverse elements and attributes in an XML document. XPath is the main element of the XSLT standard, and both XQuery and XPointer are built on top of the XPath expression.
Reference
Second, installation
PIP3 Install lxml
Third, the use
1. Import
From lxml import etree
2. Basic use
From lxml Import etreewb_data = "" " <div> <ul> <li class=" item-0 "><a href=" Link1.html ">first item</a></li> <li class=" item-1 "><a href=" link2.html ">second Item </a></li> <li class= "item-inactive" ><a href= "link3.html" >third item</a></li > <li class= "item-1" ><a href= "link4.html" >fourth item</a></li> <li class= " item-0 "><a href=" link5.html ">fifth item</a> </ul> </div>" "" "html = etree. HTML (wb_data) print (HTML) result = etree.tostring (HTML) print (Result.decode ("Utf-8"))
From the results below, our printer HTML is actually a Python object, etree.tostring (HTML) is not the whole HTML of the basic wording, complete the lack of arms and legs of the label.
<element html at 0x39e58f0>
3, to obtain the content of a tag (basic use), note, get all the contents of a tag, a will not have to add a forward slash, otherwise error.
The wording of a
html = etree. HTML (wb_data) Html_data = Html.xpath ('/html/body/div/ul/li/a ') print (HTML) for I in Html_data: print (I.text) < Element html at 0x12fe4b8>first itemsecond itemthird Itemfourth itemfifth Item
Two (just add a/text () directly after the label that needs to find the content)
html = etree. HTML (wb_data) Html_data = Html.xpath ('/html/body/div/ul/li/a/text () ') print (HTML) for I in Html_data: print (i) < Element html at 0x138e4b8>first itemsecond itemthird Itemfourth itemfifth Item
Basic use of XPath for Python crawlers