Created
April 7, 2012 16:03
-
-
Save pylemon/2329978 to your computer and use it in GitHub Desktop.
python: using mechanize to login shanbay.com
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
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() | |
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 '--'*20 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment