web-scraping - 使用 beautifulsoup 从 iframe 中获取价值
问题描述
我尝试使用 beautifulsoup从这个网站中获取温度值。但是当我打印出整个汤的文本时,它只显示了一个 iframe:
<iframe frameborder="0" height="100%" src="https://www.weatherlink.com/embeddablePage/show/c7ea9161378346e18d2e4c0ea056c55b/summary" width="100%"></iframe>
所以我尝试使用 iframe 中显示的 src 地址:但它只显示一些其他代码,没有任何我可以用漂亮的汤选择的代码 :( 这是我到目前为止的代码:
import json
from urllib.request import urlopen
from bs4 import BeautifulSoup
url= 'http://www.nordhessen-wetter.de'
# url = 'https://www.weatherlink.com/embeddablePage/show/c7ea9161378346e18d2e4c0ea056c55b/summary'
u = urlopen(url)
soup = BeautifulSoup(u, 'html.parser')
seitentxt = str(soup)
print(seitentxt)
难道不能从这段代码中得到温度值吗?
谢谢你的帮助!马吕斯
解决方案
使用页面用于获取该内容的相同 url。您可以通过开发工具在网络选项卡中找到它。
import requests
url = 'https://www.weatherlink.com/embeddablePage/summaryData/db22c5a778f14c5da538dc6f3b3ddc0d?ts=1555852879023'
r = requests.get(url).json()
units = r['currConditionValues'][0]['unitLabel']
current = str(r['currConditionValues'][0]['value']) + units
high = str(r['highLowValues'][3]['value']) + units
low = str(r['highLowValues'][4]['value']) + units
print(current, high, low)
使用convertedValue
而不是value
如果你想要逗号分隔符
温度时间:
high_time = str(r['highLowValues'][17]['value'])
print(high_time)
low_time = str(r['highLowValues'][18]['value'])
print(low_time)
推荐阅读
- python - Django 模型 ImageField 中的默认图像不起作用
- python - 从日期时间对象列表中绘制 matplotlib 中的日期和时间
- c# - 这个 Unity 脚本使游戏对象围绕一个对象旋转。如何?
- html - 为什么我的 scss 变量没有按预期解析?
- python - python套接字和vpn
- excel - 使用 Excel VB getElementsByTagName("a") 进行网页抓取仅在某些时候有效
- python - 使用 win32com 和 pandas 循环 Outlook 日历邀请
- database - 如何从转储文件中恢复 PostgreSQL 数据库
- flutter - 在颤动一定延迟后,有没有办法处理或弹出小部件?
- javascript - 最大化和最小化按钮不触发效果