"""
Author : xionghhcs
Desc : 模拟登陆豆瓣
Date : 2017-05-16 18:00:33
"""
import requests
from lxml import etree

'''
获取登陆页面的验证码图片和登陆id标识
'''
def getCaptchaImage(html):
    try:
        selector = etree.HTML(html)
        imgUrl = selector.xpath('//img[@id="captcha_image"]/@src')
        #获得captcha-id
        url = imgUrl[0]
        pos = url.find("id=")
        captcha_id = url[pos+3:pos+30]
        img = requests.get(url).content
        f = open("captcha.jpg",'wb')
        f.write(img)
        f.close()
        return captcha_id
    except:
        print('error in getCaptchaImage()')
        return ""

post_data={
    'source':'None',
    'redir':'https://www.douban.com',
    'form_email':'*******',
    'form_password':'*********',
    'captcha-solution':'stiff',
    'captcha-id':'76KRnOW3uaf4egLb4YXiqzUW:en',
    'login':'登录'
}

url = 'https://www.douban.com/accounts/login?'
session = requests.session()
r = session.get(url)
captcha_id=getCaptchaImage(r.text)
captcha = input("验证码:")
post_data['captcha-solution']=captcha
post_data['captcha-id']=captcha_id
r = session.post(url,data=post_data)
#访问个人主页，验证是否登陆成功
r = session.get("https://www.douban.com/people/137322152/")
print(r.text)

810dde4292ce1346a2bb93d54350c9e3dfa8213e
<mask token>


def getCaptchaImage(html):
    try:
        selector = etree.HTML(html)
        imgUrl = selector.xpath('//img[@id="captcha_image"]/@src')
        url = imgUrl[0]
        pos = url.find('id=')
        captcha_id = url[pos + 3:pos + 30]
        img = requests.get(url).content
        f = open('captcha.jpg', 'wb')
        f.write(img)
        f.close()
        return captcha_id
    except:
        print('error in getCaptchaImage()')
        return ''


<mask token>

<mask token>


def getCaptchaImage(html):
    try:
        selector = etree.HTML(html)
        imgUrl = selector.xpath('//img[@id="captcha_image"]/@src')
        url = imgUrl[0]
        pos = url.find('id=')
        captcha_id = url[pos + 3:pos + 30]
        img = requests.get(url).content
        f = open('captcha.jpg', 'wb')
        f.write(img)
        f.close()
        return captcha_id
    except:
        print('error in getCaptchaImage()')
        return ''


<mask token>
print(r.text)

<mask token>


def getCaptchaImage(html):
    try:
        selector = etree.HTML(html)
        imgUrl = selector.xpath('//img[@id="captcha_image"]/@src')
        url = imgUrl[0]
        pos = url.find('id=')
        captcha_id = url[pos + 3:pos + 30]
        img = requests.get(url).content
        f = open('captcha.jpg', 'wb')
        f.write(img)
        f.close()
        return captcha_id
    except:
        print('error in getCaptchaImage()')
        return ''


post_data = {'source': 'None', 'redir': 'https://www.douban.com',
    'form_email': '*******', 'form_password': '*********',
    'captcha-solution': 'stiff', 'captcha-id':
    '76KRnOW3uaf4egLb4YXiqzUW:en', 'login': '登录'}
url = 'https://www.douban.com/accounts/login?'
session = requests.session()
r = session.get(url)
captcha_id = getCaptchaImage(r.text)
captcha = input('验证码:')
post_data['captcha-solution'] = captcha
post_data['captcha-id'] = captcha_id
r = session.post(url, data=post_data)
r = session.get('https://www.douban.com/people/137322152/')
print(r.text)

<mask token>
import requests
from lxml import etree
<mask token>


def getCaptchaImage(html):
    try:
        selector = etree.HTML(html)
        imgUrl = selector.xpath('//img[@id="captcha_image"]/@src')
        url = imgUrl[0]
        pos = url.find('id=')
        captcha_id = url[pos + 3:pos + 30]
        img = requests.get(url).content
        f = open('captcha.jpg', 'wb')
        f.write(img)
        f.close()
        return captcha_id
    except:
        print('error in getCaptchaImage()')
        return ''


post_data = {'source': 'None', 'redir': 'https://www.douban.com',
    'form_email': '*******', 'form_password': '*********',
    'captcha-solution': 'stiff', 'captcha-id':
    '76KRnOW3uaf4egLb4YXiqzUW:en', 'login': '登录'}
url = 'https://www.douban.com/accounts/login?'
session = requests.session()
r = session.get(url)
captcha_id = getCaptchaImage(r.text)
captcha = input('验证码:')
post_data['captcha-solution'] = captcha
post_data['captcha-id'] = captcha_id
r = session.post(url, data=post_data)
r = session.get('https://www.douban.com/people/137322152/')
print(r.text)

"""
Author : xionghhcs
Desc : 模拟登陆豆瓣
Date : 2017-05-16 18:00:33
"""
import requests
from lxml import etree

'''
获取登陆页面的验证码图片和登陆id标识
'''
def getCaptchaImage(html):
    try:
        selector = etree.HTML(html)
        imgUrl = selector.xpath('//img[@id="captcha_image"]/@src')
        #获得captcha-id
        url = imgUrl[0]
        pos = url.find("id=")
        captcha_id = url[pos+3:pos+30]
        img = requests.get(url).content
        f = open("captcha.jpg",'wb')
        f.write(img)
        f.close()
        return captcha_id
    except:
        print('error in getCaptchaImage()')
        return ""

post_data={
    'source':'None',
    'redir':'https://www.douban.com',
    'form_email':'*******',
    'form_password':'*********',
    'captcha-solution':'stiff',
    'captcha-id':'76KRnOW3uaf4egLb4YXiqzUW:en',
    'login':'登录'
}

url = 'https://www.douban.com/accounts/login?'
session = requests.session()
r = session.get(url)
captcha_id=getCaptchaImage(r.text)
captcha = input("验证码:")
post_data['captcha-solution']=captcha
post_data['captcha-id']=captcha_id
r = session.post(url,data=post_data)
#访问个人主页，验证是否登陆成功
r = session.get("https://www.douban.com/people/137322152/")
print(r.text)
