【发布时间】: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