【python2.7】使用requests模块登陆豆瓣

不需要user-agent伪装就可以登陆成功,测试时间:2017-11-04

# -*- coding: utf-8 -*-
import requests
import re
from bs4 import BeautifulSoup

'''
新建一个Session实例,发送get请求,判断是否有captcha
'''

s = requests.Session()
url_login = 'https://accounts.douban.com/login'
response = s.get(url_login)
# print response.request.headers
soup = BeautifulSoup(response.text, 'html.parser')
captcha = soup.find('img', id='captcha_image')

if captcha:
    captcha_url = captcha['src']
    print captcha_url
    captcha_text = raw_input('Please input the captcha:')
    re_captcha_id = r'<input type="hidden" name="captcha-id" value="(.*?)"/'
    captcha_id = re.findall(re_captcha_id, response.text)

    form_data = {
        'redir': 'https://www.douban.com/people/48373115/',
        'form_email': '15861591989',
        'form_password': 'cloud523',
        'captcha-solution': captcha_text,
        'captcha-id': captcha_id,
        # 'login': u'登陆'
    }
    r = s.post(url_login, data=form_data)
    with open("test.html", 'w') as fh:
        fh.write(r.text.encode("utf-8"))
        fh.close
else:
    form_data = {
        'redir': 'https://www.douban.com/people/48373115/',
        'form_email': '15861591989',
        'form_password': 'cloud523',
    }
    r = s.post(url_login, data=form_data)
    with open("test.html", 'w') as fh:
        fh.write(r.text.encode("utf-8"))
        fh.close

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,234评论 19 139
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,707评论 0 17
  • http://192.168.136.131/sqlmap/mysql/get_int.php?id=1 当给sq...
    xuningbo阅读 10,479评论 2 22
  • sqlmap用户手册 说明:本文为转载,对原文中一些明显的拼写错误进行修正,并标注对自己有用的信息。 ======...
    wind_飘阅读 2,136评论 0 5
  • 一、基础知识篇:Http Header之User-AgentUser Agent中文名为用户代理,是Http协议中...
    iPhone阅读 15,959评论 0 13