In [24]:
import urllib2
height = 338276
url = 'http://blockexplorer.com/b/%s' % height
req = urllib2.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
f = urllib2.urlopen(req)
html = f.read()
In [47]:
from bs4 import BeautifulSoup
from datetime import datetime
def get_timestamp(s):
dt = datetime.strptime(s, '%Y-%m-%d %H:%M:%S')
epoch = datetime(1970,1,1)
secs_since_epoch = (dt - epoch).total_seconds()
return secs_since_epoch
def get_block_from_html(html):
soup = BeautifulSoup(html)
block = dict()
block['height'] = str(soup.h1.contents[0].split()[1])
for li in soup.find_all('li'):
attr = str(li.contents[0])
if attr == 'Difficulty':
value = str(li.contents[2])
index = value.find(' (')
value = value[2:index]
value = value.replace(' ', '')
else:
value = str(li.contents[-1])
if value[0] == ':':
value = value[2:]
if attr == 'Time':
block['timestamp'] = get_timestamp(value)
if attr == 'Difficulty':
block['difficulty'] = value
if attr == 'Transactions':
block['num_txs'] = value
return block
In [52]:
import grequests
def get_url(height):
url = 'http://blockexplorer.com/b/%s' % height
return url
def write_block(response, *args, **kwargs):
print response
heights = [0, 1, 2, 3]
url = 'http://blockexplorer.com/b/%s' % height
headers = {'User-Agent': 'Mozilla/5.0'}
hooks = {'response': write_block}
rs = (grequests.get(get_url(height), headers=headers, hooks=hooks) for height in heights)
grequests.map(rs)
Out[52]: