python beautifulsoup 只返回键 {} 而不是值

标签 python web-scraping beautifulsoup

您好,我今天花了几个小时从这个网站上抓取一些数据:http://www.buienradar.nl/weer/kingston/jm/3489854/5daagse 我尝试获取橙色框内的数据。 weather data

我在 python 3 上使用 bs4

无论我尝试什么,我只会得到类似键的结果,如 {temperature} 我如何获得值(value)?

from bs4 import BeautifulSoup
import requests
url = "http://www.buienradar.nl/weer/kingston/jm/3489854/5daagse"
r = requests.get(url)
soup = BeautifulSoup(r.text, "html.parser")
letters = soup.find_all("div", class_="forecast")

tempe = soup.find(class_='temperature').attrs
print(tempe)
table = soup.find(class_='precipitation').attrs
print(table)
heds = soup.find_all('table')
for h in heds:
    m = h.find_all('td')
    print(m)
    for o in m:
        print(o.text)

结果是:

   {'class': ['temperature']}
{'class': ['precipitation']}
[<td>{time}</td>, <td><img data-url="/resources/images/icons/weather/30x30/{iconcode}.png" src=""/></td>, <td><span class="temperature">{temperature}°C</span></td>, <td>{feeltemperature}°C</td>, <td>{winddirection} {beaufort}</td>, <td style="text-align:left;"><img data-url="/resources/images/icons/wind/{winddirection}.png" src="" style="width:20px;"/></td>, <td class="precipitation">{precipation}%</td>, <td>{precipationmm} mm</td>, <td>{sunshine}%</td>]
{time}

{temperature}°C
{feeltemperature}°C
{winddirection} {beaufort}

{precipation}%
{precipationmm} mm
{sunshine}%

Process finished with exit code 0

我做错了什么?提前致谢。

感谢我启动并运行它的答案:

from selenium.webdriver.support.ui import WebDriverWait
from bs4 import BeautifulSoup
from selenium.webdriver.common.by import By
import requests
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from time import sleep
driver = webdriver.Firefox(executable_path=r'path/to/selenium')
url = "http://www.buienradar.nl/weer/kingston/jm/3489854/5daagse"
driver.get(url)
WebDriverWait(driver, 10).until( EC.visibility_of_element_located((By.CLASS_NAME, "forecast")))
print("access")
sleep(1)
html_page = driver.page_source
driver.quit()

soup = BeautifulSoup(html_page, "lxml")
letters = soup.find_all("div", class_="forecast")

tempe = soup.find(class_='temperature').attrs
print(tempe)
table = soup.find(class_='precipitation').attrs
print(table)
heds = soup.find_all('table')
for h in heds:
    m = h.find_all('td')
    print(m)
    for o in m:
        print(o.text)

最佳答案

如果您直接向站点 api 发出请求,则无需 Selenium 即可更快地获取所需数据:

import requests

url = 'https://api.buienradar.nl/data/forecast/1.1/all/3489854'
# Get json response
data = requests.get(url).json()
# Parse json response
for day in data['days']:
    if 'hours' in day:
        print(day['date'])
        for hour in day['hours']:
            print('Hour - {}.00 and Precipitation - {} mm'.format(hour['hour'], hour['precipationmm']))

# 2017-05-05T00:00:00
# Hour - 21.00 and Precipitation - 0.0 mm
# Hour - 22.00 and Precipitation - 0.0 mm
# Hour - 23.00 and Precipitation - 0.0 mm
# 2017-05-06T00:00:00
# Hour - 0.00 and Precipitation - 0.0 mm
# Hour - 1.00 and Precipitation - 0.0 mm
# Hour - 2.00 and Precipitation - 0.0 mm

关于python beautifulsoup 只返回键 {} 而不是值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43767171/

相关文章:

python - webroot 上的 django urlconf 404

python - 定义一个 xml 来表示图的邻接列表?

python - 使用selenium在.click()之后获取新的html

python - 找出运行 lambda 函数的 IP

python - PyQt5 QWebEnginePage - 可以编辑 HTML 以打开下拉列表吗?

python - 抓取 : SSL: CERTIFICATE_VERIFY_FAILED error for http://en. wikipedia.org

python - Python 3 Web Scraping 中的问题 HTTP 错误 403

python - 使用 BeautifulSoup 将 <a> 定位到特定属性

python - BeautifulSoup 嵌套标签

python - 有没有办法控制pygame中矩形的大小?