求大佬解决
python3吧
全部回复
仅看楼主
level 2
-夏晚秋 楼主
import requests
from lxml import html
url = "https://www.aqistudy.cn/historydata/"
header = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)"
" Chrome/88.0.4324.182 Safari/537.36"}
response_text = requests.get(url=url, headers=header).text
# 数据解析
tree = html.etree.HTML(response_text)
div_hot = tree.xpath(".//div[@class='hot']/div/ul")[0]
hot_city_names = []
for div_hot_list in div_hot:
div_hot_name = div_hot.xpath("./li/a/text()")[0]
hot_city_names.append(div_hot_name)
# 所有城市名称
div_all = tree.xpath(".//div[@class='all']/div/ul/div[2]/li")
for div_all_list in div_all:
div_all_name = div_all.xpath("./a/text()")[0]
hot_city_names.append(div_all_name
print(hot_city_names, len(hot_city_names))
出现错误:'list' object has no attribute 'xpath' 怎么解决
2021年02月24日 08点02分 1
level 2
-夏晚秋 楼主
div_hot = tree.xpath(".//div[@class='hot']/div/ul")
这是原码
2021年02月24日 08点02分 2
level 5
你好,对Python感兴趣的朋友可以加q交流裙哦:821189983
群内每天都会更新Python资料和源码,回复:g 即可免费领取。
还有大神专业指导哦! 坐等你来!!
2021年02月25日 03点02分 3
1