Web-scraping from Coinbase with Python (Pycharm)

0 votes

I am trying to write some code that will give me the price in BTC when I run it. 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)
Sep 4, 2018 in Blockchain by slayer
• 29,050 points

retagged Nov 22, 2018 by Kalgi 274 views

1 answer to this question.

0 votes

Data that you want to scrape is dynamically 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'
answered Sep 4, 2018 by digger
• 26,550 points

Related Questions In Blockchain

0 votes
1 answer

How to interact with blockchain from web application?

You can use the Web3.js library to interact ...READ MORE

answered Jan 11 in Blockchain by Omkar
• 67,480 points
57 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Python request module for bitcoin json rpc

This should work: #!/usr/bin/env python import getpass import json import requests ...READ MORE

answered Aug 28, 2018 in Blockchain by digger
• 26,550 points
232 views
0 votes
1 answer

Cant locate english.txt file while intalling Python Bitcoin module

This is related to a pybitcointools bug where the ...READ MORE

answered Aug 30, 2018 in Blockchain by slayer
• 29,050 points
39 views
0 votes
1 answer
0 votes
1 answer

How to interact with blockchain using Java web app?

You can interact with the blockchain using ...READ MORE

answered Jul 16, 2018 in Blockchain by digger
• 26,550 points
187 views
0 votes
1 answer