代码运行错误:AttributeError: 'NoneType' object has no attribute 'find'
相关代码
import requests
from bs4 import BeautifulSoup
# 发起网络请求
url = "http://lishi.tianqi.com/nanning/index.html"
response = requests.get(url)
# 解析HTML内容
soup = BeautifulSoup(response.content, "html.parser")
# 提取日期、时间和天气数据
data = []
table = soup.find("div", class_="tqtongji2").find("table")
for row in table.find_all("tr")[1:]:
date = row.find_all("td")[0].text.strip()
time = row.find_all("td")[1].text.strip()
weather = row.find_all("td")[2].text.strip()
data.append((date, time, weather))
# 打印数据
for item in data:
print(item)
运行结果
Traceback (most recent call last):
File "C:\Users\26749\PycharmProjects\pythonProject2\1111.py", line 13, in <module>
table = soup.find("div", class_="tqtongji2").find("table")
AttributeError: 'NoneType' object has no attribute 'find'
想要达到:代码运行正常
应该被反爬了,加上请求头,可以输出一下状态码
soup的内容是none,你先从response.content开始看,看到底收到个什么数据