设为首页 加入收藏

TOP

爬虫必备—requests(四)
2017-09-30 15:29:49 】 浏览:6124
Tags:爬虫 必备 requests
rint
(i3.text) 参数示例

6. requests模拟登陆GitHub

 1 import requests
 2 from bs4 import BeautifulSoup
 3 
 4 
 5 def login_github():
 6     """
 7     通过requests模块模拟浏览器登陆GitHub
 8     :return: 
 9     """
10     # 获取csrf_token
11     r1 = requests.get('https://github.com/login')   # 获得get请求的对象
12     s1 = BeautifulSoup(r1.text, 'html.parser')      # 使用bs4解析HTML对象
13     token = s1.find('input', attrs={'name': 'authenticity_token'}).get('value')     # 获取登陆授权码,即csrf_token
14     get_cookies = r1.cookies.get_dict()     # 获取get请求的cookies,post请求时必须携带
15     
16     # 发送post登陆请求
17     '''
18     post登陆参数
19     commit    Sign+in
20     utf8    ?
21     authenticity_token    E961jQMIyC9NPwL54YPj70gv2hbXWJ…fTUd+e4lT5RAizKbfzQo4eRHsfg==
22     login    JackUpDown(用户名)
23     password    **********(密码)
24     '''
25     r2 = requests.post(
26         'https://github.com/session',
27         data={
28             'commit': 'Sign+in',
29             'utf8': '?',
30             'authenticity_token': token,
31             'login': 'JackUpDown',
32             'password': '**********'
33         },
34         cookies=get_cookies     # 携带get请求的cookies
35                        )
36     login_cookies = r2.cookies.get_dict()   # 获得登陆成功的cookies,携带此cookies就可以访问任意GitHub页面
37 
38     # 携带post cookies跳转任意页面
39     r3 = requests.get('https://github.com/settings/emails', cookies=login_cookies)
40     print(r3.text)

 

 

转载自:

1. http://www.cnblogs.com/wupeiqi/articles/6283017.html

首页 上一页 1 2 3 4 下一页 尾页 4/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Python网络爬虫与信息提取(一) 下一篇32、进程池与回调函数

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目