无法使用beautifulsoup模块python从HTML检索温度值 [英] Unable to retrieve temperature value from HTML using beautifulsoup module python
问题描述
我正在使用BeautifulSoup4解析此HTML(查看源:
weather.com网站使用其内部API将天气数据作为JSON检索.您将需要复制他们的请求.
如果您多次使用此工具,我建议您考虑使用面向开发人员的官方天气API:仅使用Google weather api
I am using BeautifulSoup4 to parse this HTML (view-source:https://weather.com/en-IN/weather/today/l/17.39,78.49) and I'm trying to retrieve temperature value. But, the value might be stored in attribute obs.temperature. I can see the temperature value "24" in the page Inspect window, but the same can't be found directly in view page source. Below is the snapshot of this problem.
From view source:
<div class="today_nowcard-temp">
<span data-gm-wx-temperature="::todayWxcardVm.obs.temperature" data-text-to-replace="{{ '[[ obs.temperature ]]'.indexOf('\[\[') !== -1 ? '--' : '[[ obs.temperature ]]' }}">[[ obs.temperature ]]</span>
</div>
From Inspect window
<div class="today_nowcard-temp">
<span data-gm-wx-temperature="::todayWxcardVm.obs.temperature" data-text-to-replace="--"><!-- ngIf: tempPrefix --> <!-- ngIf: hasValue --><span data-ng-if="hasValue" class="dir-ltr" data-ng-bind="temp | safeDisplay">24</span><!-- end ngIf: hasValue --><!-- ngIf: hasValue --><sup data-ng-if="hasValue" class="deg dir-ltr">°</sup><!-- end ngIf: hasValue --><!-- ngIf: showTempUnit -->
<!-- ngIf: !hasValue --></span>
</div>
Please let me know how to get temperature value.
Below is my Python code:
import bs4, requests
web = requests.get("https://weather.com/en-IN/weather/today/l/17.39,78.49")
websoup = bs4.BeautifulSoup(web.text, "html.parser")
print(type(websoup))
webtemperature = websoup.select("div .today_nowcard-temp span")
print(webtemperature)
print("from weather.com: "+webtemperature[0].getText()+ "degree celsius\n")
Output:
<class 'bs4.BeautifulSoup'>
[<span data-gm-wx-temperature="::todayWxcardVm.obs.temperature" data-text-to-replace="{{ '[[ obs.temperature ]]'.indexOf('\\[\\[') !== -1 ? '--' : '[[ obs.temperature ]]' }}">[[ obs.temperature ]]</span>]
from weather.com: [[ obs.temperature ]]degree celsius
BS4 is working as expected, as [[ obs.temperature ]]
is part of the html templating language they are using the build the page after it is loaded, so you can't grab it straight from the HTML as the full page must be executed. Or:
When you load the page, the temperatures appear to load afterwards as there is a loading spinner. See the network inspector:
The weather.com website uses their internal APIs for retrieving the weather data as JSON. You will need to replicate their request.
If you are using this several times, I'd recommend looking into using an official weather API for developers: just google weather api
这篇关于无法使用beautifulsoup模块python从HTML检索温度值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!