【问题标题】:Python Beautifulsoup not finding text of HTML dataPython Beautifulsoup 找不到 HTML 数据的文本
【发布时间】:2021-03-07 13:22:42
【问题描述】:

我正在尝试发出从特定网站获取数据并在控制台上打印出来的网络请求。 为此,我使用 Beautifulsoup 和 requests。您可以从下面的网站看到 HTML 数据,但我没有得到您在下面看到的 2 个坐标的输出。我不知道是否有必要提一下,网站上的 2 个坐标在不断变化。谢谢你帮助我!

HTML:

<div class="cockpitItem">
     <h3>Bodenpunkt</h3>
   <p id="gpt">
    "42,67° Nord" #Coordinate 1 that I want to get
    <br>
    "170,38° Ost" #Coordinate 2 that I want to get
  </p>
</div>

Python 代码:

url = "https://www.astroviewer.net/iss/de/"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")

body = soup.find("p",{"id": "gpt"})

print(f"HTML: {body}")
print(f"Text: {body.text}")

输出:

HTML: <p id="gpt"> <br/> </p> #It finds the right part of the HTML data
Text:                

【问题讨论】:

  • 我认为问题在于页面在不断更新,我认为 BeatifulSoup 不适合。

标签: python html beautifulsoup python-requests


【解决方案1】:

我认为它正在从https://www.astroviewer.net/iss/ws/orbit.php 轮询该数据。如本文件中所述:https://www.astroviewer.net/iss/javascript/orbit2.js。见function requestOrbitData()。它返回你可以解析的 json。

我原以为会有一个 API 可以为您提供这些数据。您还可以设置一些东西来轮询数据,但可能不那么频繁。

【讨论】:

    【解决方案2】:

    您的代码没问题,应该可以工作。 在我的机器上运行你的 sn-p,发现返回是空的,这就是你什么都看不到的原因。

    如果您想查看请求,请使用:

    print(soup.prettify())
    

    检查您的 GET 请求。

    【讨论】:

      【解决方案3】:

      您的代码适用于给定的 html。您确定您正在访问 html 的正确部分吗?请参阅下面的可重现示例。还有,只要页面的html结构不变,坐标变了也没关系。

      from bs4 import BeautifulSoup
      
      html_text = '''
      <div class="cockpitItem">
           <h3>Bodenpunkt</h3>
         <p id="gpt">
          "42,67° Nord"
          <br>
          "170,38° Ost"
        </p>
      </div>
      '''
      
      soup = BeautifulSoup(html_text, 'html.parser')
      body = soup.find('p', {'id': 'gpt'})
      print(f"Text: {body.text.strip()}")
      

      输出:

      Text: "42,67° Nord"
      
          "170,38° Ost"
      

      【讨论】:

        【解决方案4】:

        这是因为“gpt div”的默认值为空,而他的内容是由javascript更新的。

        不幸的是,在这种情况下你不能使用 requests 库,你必须使用一个包含导航器的库,比如我最熟悉的一个是 Selenium

        【讨论】:

          【解决方案5】:

          您可以通过简单的 API 调用轻松模拟该网页上的内容。

          方法如下:

          from datetime import datetime
          import time
          
          import requests
          
          while True:
              iss_position = requests.get(
                  "http://api.open-notify.org/iss-now.json",
              ).json()["iss_position"]
              output = [
                  round(float(iss_position['latitude']), 2),
                  round(float(iss_position['longitude']), 2),
                  datetime.utcnow().time().strftime('%H:%M:%S'),
              ]
              print("{:>5}° Nord {:>5}° Ost {:>5} UTC".format(*output))
              time.sleep(1)
          

          样本输出:

          49.58° Nord 167.23° Ost 14:42:54 UTC
          49.61° Nord 167.37° Ost 14:42:56 UTC
          49.63° Nord 167.51° Ost 14:42:57 UTC
          49.66° Nord 167.64° Ost 14:42:59 UTC
          49.69° Nord 167.78° Ost 14:43:00 UTC
          49.72° Nord 167.92° Ost 14:43:02 UTC
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-07-21
            • 2018-10-05
            • 2015-02-20
            • 1970-01-01
            • 2013-01-29
            • 2022-01-20
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多