xpath的|
相当与交集
本爬虫爬取的是热门城市和全国城市,但是由于爬取的规则不同,所以在同一个xpath中使用了两种规则
import requests
from lxml import etree
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
}
def work():
url='https://www.aqistudy.cn/historydata/'
res=requests.get(url=url,headers=headers).text
tree=etree.HTML(res)
#xpath特殊用法|并集
a_s=tree.xpath("//div[@class='bottom']/ul/li/a | //div[@class='bottom']/ul/div[2]/li/a")
for i in a_s:
city_name = i.xpath("./text()")[0]
print(city_name)
print(len(a_s))
if __name__ == '__main__':
work()