【问题标题】:Scrape a variable from web server with BeautifulSoup使用 BeautifulSoup 从 Web 服务器中抓取变量
【发布时间】:2021-09-18 14:25:40
【问题描述】:

我只想从每隔几秒更新一次的网络服务器中提取摄氏温度变量。到目前为止我的代码是:

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">&deg;C</sup>
  </p>

你能帮我刮一下摄氏变量吗?

【问题讨论】:

标签: python beautifulsoup


【解决方案1】:

由于温度在第二个跨度标签中,即&lt;span id="temperaturec"&gt;19.44&lt;/span&gt;,我们将使用soup.find_all访问此标签并使用.string打印其中的内容

from bs4 import BeautifulSoup as bs

html_doc = """
<!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">&deg;C</sup>
  </p>
</div>"""


soup = bs(html_doc,'html.parser')
list_of_spans = soup.find_all('span')
print(list_of_spans[1].string)

【讨论】:

  • 很高兴知道这篇文章缺少什么。
【解决方案2】:
float(soup.find("span", {"id": "temperaturec"}).text)

【讨论】:

    【解决方案3】:

    是的。您可以这样做:

    celsius = soup.find("span", {"id": "temperaturec"}).text
    

    【讨论】:

      猜你喜欢
      • 2014-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-09
      • 2019-05-27
      • 2016-10-28
      • 1970-01-01
      相关资源
      最近更新 更多