【发布时间】:2021-04-14 12:28:50
【问题描述】:
试图从 Weather Underground 中抓取一些天气数据。在获取日期/日期、高/低温度和预报(即“部分多云”)之前,我获取感兴趣的数据没有任何困难。每个都在一个没有类的 div 中。每个的父级是一个 class="obs-date" 的 div(见下图)
[WxUn HTML 图像][1]
以下尝试的代码已注释掉其他选项。每个都返回一个空列表。
def get_wx(city, state):
city=city.lower()
state=state.lower()
# get current conditions; 'weather' in url
current_dict = get_current(city, state)
# get forecast; 'forecast' in url
f_url = f'https://www.wunderground.com/forecast/us/{state}/{city}'
f_response = req.get(f_url)
f_soup = BeautifulSoup(f_response.text, 'html.parser')
cast_dates = f_soup.find_all('div', class_="obs-date")
# cast_dates = f_soup.find_all('div', attrs={"class":"obs-date"})
# cast_dates = f_soup.select('div.obs-date')
print(cast_dates)
get_wx("Portland", "ME")
对我所缺少的任何帮助表示感谢。
【问题讨论】:
-
请以您正在使用的 html 文档的文本形式分享一个 minimal 示例。
-
OpenWeatherMap 使用其API 将天气作为 JSON 数据提供,这要简单得多。
-
也许获取所有
divs并使用 Python 中的索引来获得预期的div-all_divs[0],all_divs[1], -
问题可能是:页面使用
JavaScript向HTML 添加元素,但BeautifulSoup无法运行JavaScript。它可能需要使用Selenium来控制可以运行JavaScript的真实网络浏览器。 -
您可以随时嵌套
find/find_all。您可以使用cast_dates[0].find_all(...)仅在第一个cast_dates内部搜索 - 您可以使用for-loop 对所有元素重复它。for item in cast_dates: item.find_all(...)
标签: python beautifulsoup