Error in printing the real price and currency "AttributeError: 'NoneType' object has no attribute 'text'"

Error in printing the real price and currency "AttributeError: 'NoneType' object has no attribute 'text'"

我想使用 python 打印比特币的真实价格,但是在提取货币名称及其数量时,出现 AttributeError: 'NoneType' object has no attribute 'text'

错误

这是我使用的代码:

from bs4 import BeautifulSoup as BS
import requests

url = "https://api.coinbase.com/v2/prices/ETH-CAD/buy"
data = requests.get(url)
soup = BS(data.text, 'html.parser')
ans = soup.find("amount", class_="currency").text

在最后一行,它给我一个错误 AttributeError: 'NoneType' object has no attribute 'text' 我该如何解决这个问题?

你下载那个页面并亲自看过了吗?它根本没有 return HTML。它 returns JSON。你不需要 BeautifulSoup.

url = "https://api.coinbase.com/v2/prices/ETH-CAD/buy"
data = requests.get(url)
ans = json.loads(data.text)