【发布时间】:2015-08-15 20:01:14
【问题描述】:
我写了以下python代码:
from bs4 import BeautifulSoup
import urllib2
url= 'http://www.example.com'
page = urllib2.urlopen(url)
soup = BeautifulSoup(page.read(),"html.parser")
freq=soup.find('div', attrs={'id':'frequenz'})
print freq
结果是:
<div id="frequenz" style="font-size:500%; font-weight: bold; width: 100%; height: 10%; margin-top: 5px; text-align: center">tempsensor</div>
当我使用网络浏览器查看此站点时,网页显示的是动态内容,而不是字符串“tempsensor”。温度值每秒自动刷新一次。所以网页中的内容是 自动将字符串“tempsensor”替换为数值。
我现在的问题是:如何让 Python 显示更新后的数值? BeautifulSoup中如何获取自动更新到tempsensor的值?
【问题讨论】:
-
"如何通过 python 评估变量 tempsensor 的值?" - 我没有看到任何变量...
-
实际网址是什么?
-
啊,我明白了。 JavaScript 正在更新您想要的值。您不能只为它解析 HTML。您需要弄清楚 API。
-
看起来你只是请求
http://www.netzfrequenz.info/json/aktuell2.json?_=<unix_time_in_milliseconds>。例如,netzfrequenz.info/json/aktuell2.json?_=1439677724960。