python - 使用 BeautifulSoup 从 Web 服务器中抓取变量
问题描述
我只想从每隔几秒钟更新一次的网络服务器中提取摄氏温度变量。到目前为止,我的代码是:
from bs4 import BeautifulSoup
from urllib.request import urlopen
url = "http://192.168.251.184"
page = urlopen(url)
html = page.read().decode("utf-8")
soup = BeautifulSoup(html, "html.parser")
print(soup.get_text())
但这也会打印原始 html 格式的数据:
我曾尝试使用 find() 函数来打印变量,即 19.44,但没有成功。
此变量的页面源的摘录是(第 3 行最后一行的目标变量是 19.44):
<!DOCTYPE HTML><html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<style>
html {
font-family: Arial;
display: inline-block;
margin: 0px auto;
text-align: center;
}
h2 { font-size: 3.0rem; }
p { font-size: 3.0rem; }
.units { font-size: 1.2rem; }
.ds-labels{
font-size: 1.5rem;
vertical-align:middle;
padding-bottom: 15px;
}
</style>
</head>
<body>
<h2>ESP DS18B20 Server</h2>
<p>
<i class="fas fa-thermometer-half" style="color:#059e8a;"></i>
<span class="ds-labels">Temperature Celsius</span>
<span id="temperaturec">19.44</span>
<sup class="units">°C</sup>
</p>
你能帮我刮一下摄氏变量吗?
解决方案
是的。您可以这样做:
celsius = soup.find("span", {"id": "temperaturec"}).text
推荐阅读
- python - 将轴标题添加到 3d 曲面图袖扣和 plotly
- .net-core - 有没有办法为 .NET Core 搭建脚手架测试?
- android - 此商家未启用 google pay
- node.js - 未使用 regedit 将值设置为注册表
- java - jOOQ:从外键获取引用表
- ios - 如何为选定的 tableView 行创建自定义图像复选标记附件?
- laravel-5.8 - 如何设置路径以便它们在本地主机和远程服务器上工作
- python-3.x - 优化对称测试
- python - 日期时间写道 1899-12-30:如何删除它?
- arrays - 从 Select-Object 启动一个数组