|  | 
 
| 
from  lxml import etree
x
马上注册,结交更多好友,享用更多功能^_^您需要 登录 才可以下载或查看,没有账号?立即注册  import requests
 if __name__ == '__main__':
 url='https://www.aqistudy.cn/historydata/'
 headers={
 'User-Agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Mobile Safari/537.36'
 }
 all_hotcity=[]
 all_allcity=[]
 page_data=requests.get(url=url,headers=headers).text
 tree=etree.HTML(page_data)
 hot_li_list=tree.xpath('//div[@class="bottom"]/ul/li')
 for hot_li in hot_li_list:
 hot_city=hot_li.xpath('./a/text()')[0]
 all_hotcity.append(hot_city)
 all_ul_list=tree.xpath('//div[@class="bottom"]/ul')
 for all_ul in all_ul_list:
 all_li_list=all_ul.xpath('./div[2]/li')
 for li in all_li_list:
 all_city=li.xpath('./a/text()')[0]
 all_allcity.append(all_city)
 print(all_hotcity)
 print(all_allcity)
 
 为什么所有城市和热门城市都可以用div的class属性定位,所有城市的div class在后面,使用该定位的时候不会定位到前一个div class吗
 
因为这里热门城市在 类 bottom 所属标签下的 ul 标签
 
 xpath 会获取所有符合条件的标签构成一个序列对象
 
 不管前后只好符合条件都会在这个序列对象中
 
 最后通过 for 循环就能依次读取该序列中符合条件的标签,以便后续进行数据提取
 
 
 | 
 
  |