模拟豆瓣登录

# -*- coding:utf-8 -*-
import requests
from HTMLParser import HTMLParser
from PIL import Image
class doubancilnt(object):
def __init__(self):
headers={'Referer':'https://www.google.com.ph/',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.87 Safari/537.36'}
self.session =requests.session()
self.session.headers.update(headers)
def login(self,username,password,
source='index_nav',
redir = 'https://www.douban.com/',
login='登录',
):
url='https://accounts.douban.com/login'
r = self.session.get(url)
captcha_url,captcha_id =_get_captcha(r.content)
print captcha_id
print captcha_url
if captcha_id:
img_html=self.session.get(captcha_url)
with open('captcha.jpg','wb') as f:
f.write(img_html.content)
try:
im=Image.open('captcha.jpg')
im.show()
im.close()
except:
print 'error'
captcha_solution = raw_input ('please imput solution %s:'%captcha_url)





data={ 'source':source,
'redir':redir,
'form_email':username,
'form_password':password,
'login':login,}
headers={'Referer':'https://www.google.com.ph/',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.87 Safari/537.36'}
if captcha_id:
data['captacha-id']=captcha_id
data['captacha-soution']=captcha_solution
r=self.session.post(url,data=data,headers=headers)
print self.session.cookies.items()
def _attr(attrs,attrname):
for attr in attrs:
if attr[0] == attrname:
return attr[1]
return None

def _get_captcha(content):
class CaptchaPaser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.captcha_url= None
self.captcha_id = None
def handle_starttag(self, tag, attrs):
if tag =='img'and _attr(attrs,'id')=='captcha_image' and _attr(attrs,'class')=='captcha_image':
self.captcha_url=_attr(attrs,'src')
if tag=='input' and _attr(attrs,'type')=='hidden' and _attr(attrs,'name')=='captcha-id':
self.captcha_id=_attr(attrs,'value')
p=CaptchaPaser()
p.feed(content)#回解析html<div>
return p.captcha_url,p.captcha_id
if __name__=='__main__':
C=doubancilnt()
C.login('576433951@qq.com','mima')
原文地址:https://www.cnblogs.com/ZHANG576433951/p/6184887.html