首页 > 解决方案 > 使用 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)

难道不能从这段代码中得到温度值吗?

谢谢你的帮助!马吕斯

标签: web-scrapingbeautifulsoup

解决方案


使用页面用于获取该内容的相同 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)

推荐阅读