我如何在python中从网页上打印出真实数量的此类比特币及其名称



我有一个包含比特币价格的网页,例如这个页面,https://www.coinbase.com/price/keep3rv1,我想用特定的货币(例如美元(打印这个比特币的最近价格,以下是我所做的:

from bs4 import BeautifulSoup as BS
import requests
data = requests.get('https://www.coinbase.com/price/keep3rv1')
soup = BS(data.text, 'html.parser')
ans = soup.find("amount", class_="currency").text

上面代码的最后两行,我不确定,但它们运行不正确。我需要知道如何提取上述示例比特币的美元价格。

NP:当使用硬币的API时,我不能处理上面提到的硬币,因为它不受coinsbase的支持。换句话说,我不能使用命令data = requests.get('https://api.coinbase.com/v2/prices/kp3r-USD/buy'),因为coinbase不支持它。另一方面,我可以很容易地将这种方式与coinsbase支持的其他硬币一起使用,例如btc,它可以得到如下data = requests.get('https://api.coinbase.com/v2/prices/btc-USD/buy')。为此,我在这里提出了一个新问题。这意味着,这里回答的问题是打印真实价格和货币时出错;AttributeError:';非eType';对象没有属性';text'"不一样,不一样!

错误:您需要字符串中的url,还需要编写打印函数,并在最后从最后一行中删除文本

首先你需要找到将有价格。在这个过程之后,每个标签,直到你找到价格,这是关键的

它将在没有bs4的情况下完成,只使用请求作为


import request
data = requests.get(https://www.coinbase.com/price/keep3rv1)
# loop for the span tag
# in each line of data
# when you get all the span tag search for the  'AssetChartAmount__Number-sc-1b4douf-1 foyTCz' class
# this will work for this page if you need every class then you need to change search algorithms not need for bs4

这是的代码更改

from bs4 import BeautifulSoup as BS
import requests
data = requests.get('https://www.coinbase.com/price/keep3rv1')
soup = BS(data.text, 'html.parser')
ans = soup.find("amount", class_="currency")
# here the amount is the thing that the search in page any name of page 
# and after that you'll need to add the class 
print(ans)

示例:

网站有价格而不是数量和类名,如果考虑多次,类可以是任何东西,这取决于开发者


from bs4 import BeautifulSoup as BS
import requests
data = requests.get('https://www.coinbase.com/price/keep3rv1')
soup = BS(data.text, 'html.parser')
ans = soup.find("price", class_="whataver_on_that")
print(ans)

最新更新