Created
September 2, 2012 15:33
-
-
Save kblum/3600546 to your computer and use it in GitHub Desktop.
Scrape eBucks website and get balance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib, urllib2, cookielib | |
from BeautifulSoup import BeautifulSoup | |
import re | |
def get_ebucks_balance(username, password): | |
"""Scrape eBucks website to get balance and return as an integer""" | |
# create opener | |
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookielib.CookieJar())) | |
urllib2.install_opener(opener) | |
# post to login validation page | |
login_url = 'https://www.ebucks.com/web/loginValidate.do?Action=Login' | |
login_data = {'userId': username, 'password': password} | |
opener.open(login_url, urllib.urlencode(login_data)).close() | |
# get contents of home page | |
home_url = 'https://www.ebucks.com/web/eBucks/' | |
fp = opener.open(home_url) | |
page = fp.read() | |
fp.close() | |
# parse contents of home page to get the balance | |
soup = BeautifulSoup(page) | |
balance_str = soup.find('span', {'class': 'eBucksValue'}).text.__str__() | |
balance = int(re.findall('([0-9]+)', balance_str)[0]) | |
return balance |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment