Home > other >  Small white: python: xpath expression of doubt
Small white: python: xpath expression of doubt

Time:10-10

Small white expression ability is weak, not fluent, the great god of mercy, please, where I say not clear, trouble patient pointed out, I'll think of some way to clear,


Here is a video tutorial code,

I have 12 rows and analytic don't know much about the data of 20 lines,

Line # 12: host_li_list=tree. Xpath ('//div/@/ul/li ')
Line # 20: city_names_list=tree. Xpath ('//div/@/ul/div [2]/li ')

In the source code of web pages, there are two div class=bottom, but their parent is different, (see screenshot)
The first div class=bottom:
is a popular cityThe second div class=bottom: is it all into the city

Bug in the code, but in this creeper didn't write different parent, jump straight to the div (@), the program can find out, which exactly is to parse div class=bottom data?

Don't program is the first to try the first expression line 12, if found, will not go down; If not, try 20 lines?




 
The import requests
The from LXML import etree
# project requirements: parsing out all city name https://www.aqistudy.cn/historydata/
If __name__=="__main__" :
Headers={
'the user-agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10 _12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36 '
}
Url='https://www.aqistudy.cn/historydata/'
Page_text=requests. Get (url=url, headers=headers). The text

The tree=etree. HTML (page_text)
Host_li_list=tree. Xpath ('//div/@/ul/li ')
All_city_names=[]
# parse the hot city city name
For li in host_li_list:
Hot_city_name=li. Xpath ('/a/text () ') [0]
All_city_names. Append (hot_city_name)

All cities # parse is the name of the
City_names_list=tree. Xpath ('//div/@/ul/div [2]/li ')
For li in city_names_list:
City_name=li. Xpath ('/a/text () ') [0]
All_city_names. Append (city_name)

Print (all_city_names, len (all_city_names))

  • Related