Skip to content

Instantly share code, notes, and snippets.

@bkeating
Last active December 28, 2015 16:49
Show Gist options
  • Save bkeating/7531795 to your computer and use it in GitHub Desktop.
Save bkeating/7531795 to your computer and use it in GitHub Desktop.
Grab current price of bitcoin in USD from coinbase.com.
#!/usr/bin/env python
from bs4 import BeautifulSoup
import urllib2
site = "https://coinbase.com/"
# Coinbase judges you.
hdr = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.8',
'Connection': 'keep-alive'
}
req = urllib2.Request(site, headers=hdr)
try:
page = urllib2.urlopen(req)
except urllib2.HTTPError, e:
print e.fp.read()
soup = BeautifulSoup(page.read())
print soup.find_all("li", class_="top-balance")[0].a.text.strip("Current Buy Price: ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment