Created
August 15, 2017 09:58
-
-
Save WincerChan/3574fd7714e939d3d7ff4ac436c7371b to your computer and use it in GitHub Desktop.
模拟登陆豆瓣
This file contains hidden or 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 requests import session, get | |
from re import search, compile | |
from PIL import Image | |
class Douban: | |
def __init__(self): | |
self.url = 'https://accounts.douban.com/login' | |
self.head = { | |
'User-Agent': | |
('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 ' | |
'(KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36') | |
} | |
self.responce = get(self.url).text | |
self.ssion = session() | |
self.data = { | |
'source': 'None', | |
'redir': 'https://www.douban.com', | |
'form_email': '', | |
'form_password': '', | |
'login': '登录' | |
} | |
def needCatpcha(self): | |
return search(r'l>验证码', self.responce) | |
def getCatpcha(self): | |
img = compile(r'img id="captcha_image" src="(.*?)"') | |
img_url = img.findall(self.responce)[0] | |
jpg = get(img_url).content | |
with open('captcha.jpg', 'wb') as f: | |
f.write(jpg) | |
Image.open('captcha.jpg').show() | |
captcha_solu = input('input captcha: ') | |
capt = compile(r'captcha\?id=(.*?)\&') | |
captcha_id = capt.findall(img_url)[0] | |
self.data = { | |
'source': 'None', | |
'redir': 'https://www.douban.com', | |
'form_email': '', | |
'form_password': '', | |
'captcha-solution': captcha_solu, | |
'captcha-id': captcha_id, | |
'login': '登录' | |
} | |
def main(self): | |
if self.needCatpcha(): | |
self.getCatpcha() | |
self.ssion.post(self.url, headers=self.head, data=self.data) | |
else: | |
self.ssion.post(self.url, headers=self.head, data=self.data) | |
return self.ssion | |
db = Douban() | |
ssion = db.main() | |
print(ssion.get('https://www.douban.com/').text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment