Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python beatifulsoup只返回键{},而不是值_Python_Web Scraping_Beautifulsoup_Bs4 - Fatal编程技术网

python beatifulsoup只返回键{},而不是值

python beatifulsoup只返回键{},而不是值,python,web-scraping,beautifulsoup,bs4,Python,Web Scraping,Beautifulsoup,Bs4,嗨,我今天花了几个小时从这个网站上搜集了一些数据: 我试着把数据放在橙色的盒子里 我使用的是Python3和bs4 无论我尝试什么,我只得到一个类似于{temperature}的结果 如何获取值的 from bs4 import BeautifulSoup import requests url = "http://www.buienradar.nl/weer/kingston/jm/3489854/5daagse" r = requests.get(url) soup = BeautifulS

嗨,我今天花了几个小时从这个网站上搜集了一些数据: 我试着把数据放在橙色的盒子里

我使用的是Python3和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

你没有做错任何事,只是没有做浏览器所做的一切。这个网站尤其只在你获取URL时提供一个“模板”,他们依靠Javascript来填写模板值。如果你在Chrome中打开“网络”标签,你会看到一堆请求。具体来说,将执行一系列替换,包括{temperature}和{feeltemperature}。

如果您正在寻找类似temepture的东西,您将这样做:

temp = soup.findAll('span',{'class':'temperature'}) 
#It's not spelled correctly make sure you take that into account

这里的问题似乎不是代码,温度是由javascript或其他东西生成的。它是动态的,因此,您必须使用Selenium(自动浏览器)

如果您直接请求站点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

想要的输出是什么mm在橙色框中指出的见照片链接我已经提交了编辑,这不是我的观点网站的数据是动态的你不能仅仅通过页面上的get请求来获取它,我不是在寻找API,因为他要求使用scraping和beautifulSoup这是获取数据的一种方法,但它可能有一些限制/限制。
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