【问题标题】:Unable to scrape the real-time price of bitcoin using beautifulsoup无法使用beautifulsoup 抓取比特币的实时价格
【发布时间】:2020-12-11 22:48:44
【问题描述】:

我正在尝试获取比特币的实时价格。比特币的价格在网站上几乎每 5 秒变化一次,但在我的代码中,它没有更新,并且与代码抓取的第一个价格保持不变。你能帮我解释一下为什么会这样吗?

import requests
from bs4 import BeautifulSoup

url = 'https://coinmarketcap.com/currencies/bitcoin/'

for i in range(100):
    response = requests.get(url,  headers={'User-Agent': 'Mozilla/5.0'})
    soup = BeautifulSoup(response.text, 'html.parser')
    price = soup.find('span' ,attrs={"class" : "cmc-details-panel-price__price"})
    print (price)
    time.sleep(20)

我的输出:

<span class="cmc-details-panel-price__price">$18,106.79</span>
<span class="cmc-details-panel-price__price">$18,106.79</span>
<span class="cmc-details-panel-price__price">$18,106.79</span>
<span class="cmc-details-panel-price__price">$18,106.79</span>
<span class="cmc-details-panel-price__price">$18,106.79</span>
<span class="cmc-details-panel-price__price">$18,106.79</span>
<span class="cmc-details-panel-price__price">$18,106.79</span> 
<span class="cmc-details-panel-price__price">$18,106.79</span>
<span class="cmc-details-panel-price__price">$18,106.79</span>
<span class="cmc-details-panel-price__price">$18,106.79</span>
<span class="cmc-details-panel-price__price">$18,106.79</span>
<span class="cmc-details-panel-price__price">$18,106.79</span>
<span class="cmc-details-panel-price__price">$18,106.79</span> 
<span class="cmc-details-panel-price__price">$18,106.79</span>
<span class="cmc-details-panel-price__price">$18,106.79</span>

【问题讨论】:

  • 该站点正在使用实时更新,我猜是一些 javascript 等。每次刷新站点时,您都​​会得到相同的值,然后站点触发触发器来更新值。由于您的请求无法等待或与网站上的 javascript 交互,因此它总是在加载时获得第一个值。
  • 该数据来自 websocket,所以使用它。否则,您将浪费大量资源,并且您的价格会滞后于实时价格。

标签: python python-3.x web-scraping beautifulsoup python-requests


【解决方案1】:

该网站正在使用实时更新,我猜是一些 javascript。每次刷新站点时,您将获得相同的值,然后站点触发触发器以更新该值。由于您的请求无法等待或与网站上的 javascript 交互,因此它总是在加载时获得第一个值。

我的建议是使用 API,它比抓取网站更有效。 第一次 Google 搜索给出:https://www.coindesk.com/coindesk-api 作为免费的比特币 API。

查看他们的 API 端点:https://api.coindesk.com/v1/bpi/currentprice.json 给出你需要的,然后只解析 JSON。

编辑:阅读他们页面上的条款。

【讨论】:

    【解决方案2】:

    API 更好,但如果您仍想抓取它,您可以使用 Google 搜索结果执行以下操作:

    import time, requests, lxml
    from bs4 import BeautifulSoup
    
    headers = {
          "User-Agent":
          "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582"
      }
    
    # check 100 times (or use while loop instead)
    for _ in range(100):
      
      html = requests.get('https://www.google.com/search?q=bitcoin+usd', headers=headers)
      soup = BeautifulSoup(html.text, 'lxml')
    
      print(soup.select_one('.SwHCTb').text)
      time.sleep(20) # sleep for price to change
    

    输出:

    58,654.40
    58,654.40
    58,654.40
    58,654.40
    58,594.20
    58,594.20
    58,594.20
    58,586.30
    58,586.30
    ...
    

    或者,您可以使用来自 SerpApi 的 Google Direct Answer Box API 获取此信息。这是一个带有免费计划的付费 API。

    在这种情况下,不同之处在于您不必弄清楚如何绕过 Google 的阻止。

    要集成的代码:

    from serpapi import GoogleSearch
    
    params = {
      "api_key": "YOUR_API_KEY",
      "engine": "google",
      "q": "bitcoin usd",
      "gl": "us",
      "hl": "en"
    }
    
    search = GoogleSearch(params)
    results = search.get_dict()
    
    print(results['answer_box']['result'])
    
    # 60,571.40 United States Dollar
    

    免责声明,我为 SerpApi 工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      • 2016-05-12
      • 2021-07-28
      • 2021-12-20
      • 2020-07-08
      • 1970-01-01
      相关资源
      最近更新 更多