用漂亮的词组在网上拉屎,得到空的lis

2024-04-26 22:33:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我通过从https://www.wunderground.com/(随机搜索zipcode)获取基本的天气数据来练习网页浏览。在

我尝试过各种不同的代码,但它总是返回一个空的列表,其中应该是温度。老实说,我只是不知道到底哪里出了问题。谁能给我指出正确的方向吗?在

import requests
from bs4 import BeautifulSoup
response=requests.get('https://www.wunderground.com/cgi-bin/findweather/getForecast?query=76502')
response_data = BeautifulSoup(response.content, 'html.parser')
results=response_data.select("strong.high")

我还尝试了以下操作以及其他各种变体:

^{pr2}$

Tags: 数据代码httpsimportcom网页列表data
1条回答
网友
1楼 · 发布于 2024-04-26 22:33:27

您要解析的数据是由JavaScript动态创建的,requests无法处理。您应该将^{}与{a2}或任何其他驱动程序一起使用。下面是使用selenium^{}的示例:

from selenium import webdriver
from bs4 import BeautifulSoup

url='https://www.wunderground.com/cgi-bin/findweather/getForecast?query=76502'
driver = webdriver.Chrome()
driver.get(url)
html = driver.page_source

soup = BeautifulSoup(html, 'html.parser')

检查元件时,可以使用以下方法找到最低、最高和当前温度:

^{pr2}$
>>> low, high, now
('25', '37', '36.5')

相关问题 更多 >