python爬虫解析所有的城市名称


import requests
#random随机模块
import random
from lxml import etree
import os
url = 'https://www.aqistudy.cn/historydata/'
headers = {
    'Connection':'close', #当请求成功后,马上断开该次请求(及时释放请求池中的资源)
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'
}
page_text = requests.get(url=url,headers=headers).text
tree = etree.HTML(page_text)
#获取热门城市或者全部城市的xpath
li_list = tree.xpath('//div[@class="bottom"]/ul/li | //div[@class="bottom"]/ul/div[2]/li')
for li in li_list:
    #热门城市xpath
    city_name = li.xpath('./a/text()')[0]
    print(city_name)

说点什么

avatar
  Subscribe  
提醒