python - 如何使用漂亮的汤从实时网站记录数据
问题描述
您好,我正在尝试使用漂亮的汤并请求记录来自每秒实时更新的风速计的数据。这个网站的链接在这里:
http://88.97.23.70:81/
我要抓取的数据在图像中以紫色突出显示:
通过在我的浏览器中检查 html。
我已经编写了下面的代码以尝试打印出数据,但是当我运行它打印的代码时None
:我认为这意味着汤对象实际上并不包含整个 html 页面?打印时,我在浏览器中检查 html 时soup.prettify()
找不到相同的内容。id=js-2-text
如果有人知道为什么会这样或如何解决它,我将不胜感激。
from bs4 import BeautifulSoup
import requests
wind_url='http://88.97.23.70:81/'
r = requests.get(wind_url)
data = r.text
soup = BeautifulSoup(data, 'lxml')
print(soup.find(id='js-2-text'))
一切顺利,布伦丹
解决方案
数据是从外部 URL 加载的,所以 beautifulsoup 不需要它。您可以尝试使用页面连接到的 API URL:
import requests
from bs4 import BeautifulSoup
api_url = "http://88.97.23.70:81/cgi-bin/CGI_GetMeasurement.cgi"
data = {"input_id": "1"}
soup = BeautifulSoup(requests.post(api_url, data=data).content, "html.parser")
_, direction, metres_per_second, *_ = soup.csv.text.split(",")
knots = float(metres_per_second) * 1.9438445
print(direction, metres_per_second, knots)
印刷:
210 006.58 12.79049681
推荐阅读
- acumatica - 如何使用 acumatica screenID 添加新的顶级实体(以休息 Web 服务端点)?
- typescript - 在 APIGatewayProxyEvent 中,哪个字段会给我 URL?
- javascript - 如果我知道字符串的值,我可以将其直接插入剪贴板而不执行 execCommand('copy')
- android - TensorFlow Lite 2.0 高级 GPU 在 Android 上使用 C++
- c# - 身份在 .NET Core 项目中不起作用
- python - 我怎样才能永远运行一个函数?
- ios - iOS Swift 共享 PDF 文件,然后删除
- javascript - 需要 Websocket onMessage 但值未定义
- python-2.7 - lxml解析器在解析html时删除结束标记
- python - django 小部件调整表单控件而不是操作