python - 网络抓取更新值

标签 python beautifulsoup request

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

相关文章:

python - Pandas - 计算分组的天数

python - 根据字符串中的单词从 DataFrame 中删除行

python - 用 BeautifulSoup 抓取一系列表格

python - 如何合并两个漂亮的汤标签?

node.js - Nodejs instagram api post 请求授权码,它给出 err 400

python - 无法将项目导入到 Scrapy Spider [未命名模块] - Python

python - 如何使用 SQLAlchemy 核心选择除 postgresql 中 1 个特定列之外的所有表列?

python - 获取标签列表,获取BeautifulSoup中的属性值

ios - 在 Alamofire 中一次只处理一个请求?

javascript - 如何使用nodejs请求库从客户端检测服务器关闭的连接