Someone would ask what is the relationship between re and XPath? If you know JS and jquery, then this is a good understanding.
Previous: The use of Python script (re) to capture the beautiful air mm picture
#-*-coding:utf-8-*- fromlxmlImportetreehtml=""" <! DOCTYPE html> """selector=etree. HTML (HTML)############ Example 1 #############Here the id attribute is used to locate which DIV and UL are matched using text () to get the textual content#here, pay attention to the layers .#content = Selector.xpath ('//div[@id = "CCTV"]/div[@class = "Content"]/ul[@id = "ul"]/li/text () ')#content = Selector.xpath ('//div[@id = "CCTV"]/div[@class = "Content"]/ul[@id = "Ul2"]/li/span[@class = "CTV"]/text ( ) ')Content = Selector.xpath ('//a/@href') forIinchcontent:Print(i)Print(U"************ Ornate divider 1 ************")############ Example 2 #############position the title of a tag with an absolute pathcon = Selector.xpath ('/html/body/div/a/@title')#using relative path positioning the two effects are the samecon = Selector.xpath ('//a/@title')Print(Len (con))Print(Con[0], con[1])Print(U"************ Ornate Divider 2 ************")############ Example 3 #############Starts-with Resolving a Label property value starting with the same stringCon2 = Selector.xpath ('//span[starts-with (@class, "C")]/text ()')#here, use the Starts-with method to extract the div tag whose id tag attribute value starts with a. forIinchCon2:Print(i)Print(U"************ Ornate Divider 3 ************")#string (.) Label set labelHTML2 =" "<div id= "A" > Left <span id= "B" > Right <ul> up <li>down</li> </ul> East </span> West </DIV&G t;" "#The following is no output with the string methodSelector2 =etree. HTML (HTML2) Con3= Selector2.xpath ('//div[@id = "a"]/text ()') forIinchCon3:Print(i)Print(U"************ Ornate Divider 4 ************")#The following uses the output of the string methoddata = Selector2.xpath ('//div[@id = "a"]')#info = Data[0].xpath (' string (.) '). Extract () [0]info = Data[0].xpath ('string (.)') Con4= Info.replace ('\ n',"'). Replace (' ',"') forIinchCon4:#The python output defaults to \ n, wrap, and replace the end to make no line break. Print(I, end="')Print("\ r")Print(U"************ Ornate Divider 5 ************")############ Example 4 ############HTML3 ="""<div>hello <p>H</p> </div> <div>hehe</div>"""Selector3=etree. HTML (HTML3)#Use the text () method to determine which div tag isCon5 = Selector3.xpath ('//div[text () = "hehe"]/text ()')Print(con5[0])Print(U"************ Ornate Divider 6 ************")############ Example 5 ############HTML4 ="""<div id= "UTV" >hello <p>H</p> <p>J</p> <p>I</p> & Lt;/div> <div>hehe</div>"""Selector4=etree. HTML (HTML4)#in XPath, you can use the multiple filtering method to find a label, such as ul[3][@id = "A"] here use "3" to find the third UL tag and its id attribute value is aCon6 = Selector4.xpath ('//div/p[position () >=2]/text ( )') forIinchCon6:Print(i)Print(U"************ Ornate Divider 7 ************")
Fetching Data using Python script (XPath)