我是Python新手(实际上第二次尝试学习语言,所以我知道一些东西),并且我正在尝试构建一个脚本来擦除天气预报。python web scraping Weatherforecast
现在我有一个小问题找到正确的html类导入到python。我现在有这样的代码:
import requests
from bs4 import BeautifulSoup
page = requests.get("https://openweathermap.org/city/2743477")
soup = BeautifulSoup(page.content, 'html.parser')
city_name = soup.find(class_="weather-widget__city-name")
print(city_name)
问题是,这只是返回“无”
我发现,通过镀铬的代码搜索和检查网页的类。如果我通过蟒蛇用下面的代码导出HTML页面:
import requests
from bs4 import BeautifulSoup
page = requests.get("https://openweathermap.org/city/2743477")
soup = BeautifulSoup(page.content, 'html.parser')
city_name = soup.find(class_="weather-widget__city-name")
print(soup.prettify())
然后我看到在cmd中的HTML页面(如预期),但我也无法找到“类_ =‘天气widget__city名’ '所以我并不惊讶,蟒蛇也无法。我的问题是,为什么python给我的html代码不同于Chrome代码显示在网站上的html代码?我是否在尝试通过BeautifulSoup以这种方式查找天气小部件时遇到了问题?
这是一张来自页面的图片,我试图刮去的部分是用红色圈起来的。
提前感谢!
你已经得到了你的问题,三个答案,但你还是没有照顾到他们的回答回应,甚至也不觉得有必要说感谢。多么文明! – SIM