无法从 Python 中的 Beautifulsoup 中获取值 [英] can't scape a value from Beautifulsoup in python

查看:36
本文介绍了无法从 Python 中的 Beautifulsoup 中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个显示当前风的网站.当我访问 https://www.windguru.cz/station/219(并点击在最大检查元素上:{wind})我可以看到:

I am creating a website where I display the current wind. when I go to https://www.windguru.cz/station/219 (and click on inspect element at the max:{wind}) I can see this:

<span class="wgs_wind_max_value">12</span>

12 是我需要的值,但是当我尝试用 bs4 和 requests 抓取它时,这显示为输出:

the 12 is the value I need but when I try to scrape it with bs4 and requests, this appears as output:

<span class="wgs_wind_max_value"></span>

如您所见,没有12"值.有人可以帮我吗?

as you can see there is no '12' value. can someone help me with that?

from bs4 import BeautifulSoup
import requests

page = requests.get('https://www.windguru.cz/3323')
soup = BeautifulSoup(page.content, "lxml")
table = soup.find_all("span",{"class","wgs_wind_max_value"})
print(table)

推荐答案

使用与 page 相同的 API 来获取 json 来填充这些值.请注意传递给 API 的查询字符串构造.

Use the same API as page does to get json to populate those values. Notice the querystring construction passed to the API.

import requests

headers = {'Referer' : 'https://www.windguru.cz/station/219'}    
r = requests.get('https://www.windguru.cz/int/iapi.php?q=station_data_current&id_station=219&date_format=Y-m-d%20H%3Ai%3As%20T&_mha=f4d18b6c', headers = headers).json()
print(r)
print(r['wind_max'])

这篇关于无法从 Python 中的 Beautifulsoup 中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆