-1

I am trying to write some code that will give me the price in BTC when I run it. Although I am not getting an error after running the code, I am not getting the price and I am getting NONE. Can anyone look at my code and figure out what the problem is? Here is the code below:

import requests
from bs4 import BeautifulSoup

page = requests.get("https://www.coinbase.com/charts")
soup = BeautifulSoup(page.content, 'html.parser')
seven_day = soup.find(id="seven-day-forecast")
bitcoin = soup.find('pre',{'style':'word-wrap: break-word; white-space: pre-
wrap;'})

print(bitcoin)

Thanks a lot!

Jay
  • 31
  • 1
  • 9

1 Answers1

1

Data that you want to scrape is dinamycally generated. You can make direct request to API to get those values:

url = 'https://api.coinbase.com/v2/prices/USD/spot?'
response = requests.get(url).json()
print(response)

Output:

{'data': [{'currency': 'USD', 'base': 'BTC', 'amount': '7590.01'}, {'currency':
'USD', 'base': 'ETH', 'amount': '296.86'}, {'currency': 'USD', 'base': 'LTC', 'amount': '54.59'}]}

To get required value:

print(response['data'][0]['amount'])

Output:

'7590.01'
Andersson
  • 51,635
  • 17
  • 77
  • 129