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
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' 怎么解决