Created
July 17, 2016 02:01
-
-
Save kba977/dcdedac4f632458e215edac8a5b708bd to your computer and use it in GitHub Desktop.
豆瓣模拟登陆
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
# coding: utf-8 | |
import re | |
import os | |
import requests | |
class Douban: | |
def __init__(self, username, password): | |
self.username = username | |
self.password = password | |
self.s = requests.session() | |
self.login_url = 'https://accounts.douban.com/login' | |
self.text = self.get_text() | |
def get_text(self): | |
text = self.s.get(self.login_url).text | |
return text | |
def get_captcha(self): | |
text = self.text | |
m = re.search(r'id="captcha_image".+src="(.*?)"', text) | |
captha_image_url = m.group(1) | |
captha = requests.get(captha_image_url).content | |
with open('captha.jpg', 'wb') as fp: | |
fp.write(captha) | |
os.system('open captha.jpg') | |
captha_code = raw_input('Please enter captha: ') | |
return captha_code | |
def get_captcha_id(self): | |
text = self.text | |
m = re.search(r'name="captcha-id.+value="(.*)"', text) | |
return m.group(1) | |
def login(self): | |
data = { | |
'source': 'None', | |
'redir': 'https://www.douban.com', | |
'form_email': self.username, | |
'form_password': self.password, | |
'captcha-solution': self.get_captcha(), | |
'captcha-id': self.get_captcha_id(), | |
'login': u'登陆' | |
} | |
text = self.s.post(self.login_url, data).text | |
if u'帐号管理' in text: | |
return "登陆成功" | |
else: | |
return "登陆失败" | |
douban = Douban(raw_input('请输入用户名: '), raw_input('请输入密码: ')) | |
content = douban.login() | |
print content |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment