【问题标题】:Web scrape a updating value网页抓取更新值
【发布时间】:2019-07-15 04:48:18
【问题描述】:

我是 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)

【问题讨论】:

    标签: python beautifulsoup request


    【解决方案1】:

    使用 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-19
      • 2020-06-18
      • 1970-01-01
      • 1970-01-01
      • 2018-01-06
      • 1970-01-01
      • 2020-11-01
      相关资源
      最近更新 更多