Issue
I want to print the real price of bitcoins using python, but when extracting the name of the currency and its amount, I get an error of AttributeError: 'NoneType' object has no attribute 'text'
Here is the code I used:
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
in the last line, it gives me an error of AttributeError: 'NoneType' object has no attribute 'text'
How can I solve that issue?
Solution
Have you downloaded that page and looked at it yourself? It doesn't return HTML at all. It returns JSON. You don't need BeautifulSoup.
url = "https://api.coinbase.com/v2/prices/ETH-CAD/buy"
data = requests.get(url)
ans = json.loads(data.text)
Answered By - Tim Roberts
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.