我是Python新手,我正在尝试抓取一个不断更新的值。当您第一次进入该站点时,该值显示“laddar tempatur(加载温度)”,然后在一段时间后继续显示实际温度。当我运行脚本时,我得到的唯一结果是“加载温度”值。我猜测这与脚本每次运行时都会重新加载网站有关。我如何获取它,使其“停留”在网站上并在“加载温度”后收集信息?
网站:http://s-websrv02.lulea.se/ormberget/
from bs4 import BeautifulSoup
import requests
import time
r = requests.get("http://s-websrv02.lulea.se/ormberget/")
soup = BeautifulSoup(r.text, "html.parser")
match = soup.find("div", id="ReloadThis").text
for item in match:
print(match)
time.sleep(20)
最佳答案
使用 XHR 调用获取温度。
下面的代码应返回温度
import requests
r = requests.get('http://s-websrv02.lulea.se/ormberget/Orm_Stadium.php')
print(r.text.strip())
如果您想定期获取温度值,请执行以下操作:
import time
import requests
collected_data = []
SLEEP_TIME = 5
while True:
r = requests.get('http://s-websrv02.lulea.se/ormberget/Orm_Stadium.php')
value = r.text.strip() if r.status_code == 200 else '-1000'
collected_data.append({'time':time.time(), 'value':value})
time.sleep(SLEEP_TIME)
关于python - 网络抓取更新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54804253/