您好我今天花了几个小时从这个网站上搜集一些数据:http://www.buienradar.nl/weer/kingston/jm/3489854/5daagse 我尝试将数据放在橙色框内。weather data
我在python 3上并使用bs4
无论我尝试什么,我只能获得像{temperature}这样的类似键的结果 我如何获得价值?
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)
答案 0 :(得分:2)
如果您直接向网站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
答案 1 :(得分:1)
你没有做错任何事,你只是没有做浏览器正在做的一切。当您获取URL并且他们依靠Javascript填写模板值时,此网站特别仅提供“模板”。如果您打开Chrome中的“网络”标签,您会看到一堆请求。具体而言,https://static.buienradar.nl/resources/js/v/1.0.22/buienradar.min.js将执行一系列替换,包括{temperature}和{feeltemperature}。
答案 2 :(得分:0)