【问题标题】:Why is my BeautifulSoup code not working anymore when looking for something in Robinhood?在 Robinhood 中查找内容时,为什么我的 BeautifulSoup 代码不再工作?
【发布时间】:2020-04-22 13:01:50
【问题描述】:

我正在开发一个加密机器人,并且几乎完成了我的项目。数周以来,我的团队一直在寻找能够返回不断更新的 BTC 价格的 Robinhood API。下面的代码过去一周在整个团队的计算机上工作,但现在它拒绝工作。我尝试过使用不同的解析器,但无法弄清楚现在的问题是什么。它工作了这么久,现在突然拒绝工作。任何帮助将不胜感激!

from bs4 import BeautifulSoup
import requests
import json


# returns value of bitcoin from https://robinhood.com/crypto/BTC using BeautifulSoup
def getPrice():
    price = ""

    response = requests.get("https://robinhood.com/crypto/BTC")  # Returns instance of Response class
    response.encoding = 'utf-8'  # Just in case the charset of response is not recognized

    # crypto: bs4.BeautifulSoup = BeautifulSoup(response.content, 'html.parser')
    # annotation format highlights what type of class the variable crypto is
    # https://stackoverflow.com/questions/51639332/use-of-colon-in-variable-declaration
    crypto = BeautifulSoup(response.content, "html.parser")

    for digit in crypto.find_all("span", {"class": "_9YsRP4ChsxbL9qzZnKv0K up"}):  # return type of find is object
        if digit.text != '$' and digit.text != ',':
            price += digit.text

    return float(price)

【问题讨论】:

    标签: python parsing beautifulsoup python-requests


    【解决方案1】:

    如果您查看页面源代码。你要找的内容是js加载的。使用 requests.get 只会获取页面源,因此不会从 js 引擎获取任何内容。为此,您必须使用 selenium 模块中的 webdriver 等工具。您还必须安装网络驱动程序,请参见此处:https://selenium-python.readthedocs.io/installation.html

    代码:

    from selenium import webdriver
    from bs4 import BeautifulSoup
    from selenium.webdriver.firefox.options import Options
    
    def getPrice():
        options = Options()
        options.add_argument('--headless')
        driver = webdriver.Firefox(options=options)
        driver.get('https://robinhood.com/crypto/BTC')
        crypto = BeautifulSoup(driver.page_source, 'html.parser')
        price=''
        for digit in crypto.find_all("span", {"class": "_9YsRP4ChsxbL9qzZnKv0K"}):
            if digit.text != '$' and digit.text != ',':
                price += digit.text
        return float(price)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-28
      • 1970-01-01
      • 2013-10-14
      • 1970-01-01
      • 2016-05-04
      • 1970-01-01
      • 2016-07-06
      • 1970-01-01
      相关资源
      最近更新 更多