我正忙着做python教程,还忙着做一些网页抓取。当讲师运行程序时,他没有收到任何错误,但是,当我运行程序时,我收到一个属性错误。该教程是去年年底制作的,所以网站的格式有点改变。然而,我已经设法解决了这个问题。 我正在浏览网站forecast.weather.gov 奇怪的是,在那之前我运行了一条类似的线路,它工作得非常好,所以我真的很困惑! 代码如下:
import requests
from bs4 import BeautifulSoup
page = requests.get('https://forecast.weather.gov/MapClick.php?lat=34.05349000000007&lon=-118.24531999999999#.X8qd1NgzZPY')
soup = BeautifulSoup(page.content, 'html.parser')
week = soup.find(id="seven-day-forecast-body")
# print(week)
items = week.find_all(class_='forecast-tombstone')
#print(items)
print(items[1].find(class_='period-name').get_text())
print(items[1].find(class_='short-desc').get_text())
print(items[1].find(class_='temp').get_text())
period_names = [item.find(class_='period-name').get_text() for item in items]
short_descriptions = [item.find(class_='short-desc').get_text() for item in items]
temperature = [item.find(class_='temp').get_text() for item in items]
print(period_names)
print(short_descriptions)
print(temperature)
这是输出:
Overnight
Partly Cloudy
Low: 49 °F
Traceback (most recent call last):
File "C:/Users/Bongi/PycharmProjects/Qazicourse/Web Scraping.py", line 18, in <module>
temperature = [item.find(class_='temp').get_text() for item in items]
File "C:/Users/Bongi/PycharmProjects/Qazicourse/Web Scraping.py", line 18, in <listcomp>
temperature = [item.find(class_='temp').get_text() for item in items]
AttributeError: 'NoneType' object has no attribute 'get_text'
Process finished with exit code 1
请注意,这一行只有一个错误
temperature = [item.find(class_='temp').get_text() for item in items]
而不是这个,print(items[1].find(class_='temp').get_text())
我是一个完全的代码初学者(不仅仅是python),所以如果我放了太多的代码,我很抱歉。任何建议或帮助都将不胜感激
目前没有回答
相关问题 更多 >
编程相关推荐