Skip to content

Instantly share code, notes, and snippets.

@pylemon
Created April 7, 2012 16:03
Show Gist options
  • Save pylemon/2329978 to your computer and use it in GitHub Desktop.
Save pylemon/2329978 to your computer and use it in GitHub Desktop.
python: using mechanize to login shanbay.com
from mechanize import Browser
import json
LOGIN_URL = 'http://www.shanbay.com/accounts/login/'
USERNAME = ''
PASSWORD = ''
API = 'http://www.shanbay.com/api/word/'
def login(username=USERNAME, password=PASSWORD, login_url=LOGIN_URL):
print '[begin login]'
br = Browser()
br.open(login_url)
br.form = br.forms().next()
br['username'] = username
br['password'] = password
print 'logining.....'
br.submit()
print 'login success! userinfo: '
print br.open('http://www.shanbay.com/api/user/info/').get_data()
print
return br
if __name__ == '__main__':
br = login()
cmd = ''
while cmd != 'q':
cmd = raw_input('Find word: ')
resp = br.open(API+cmd).get_data()
res_dict = json.loads(resp)
print res_dict['voc']['content']
print res_dict['voc']['definition']
if res_dict['learning_id'] == 0:
print 'This word is added to shanbay.'
br.open('http://www.shanbay.com/api/learning/add/' + cmd)
print
print '--'*20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment