介绍
使用 requests 可以模拟浏览器的请求,比起之前用到的 urllib,requests 模块的 api 更加便捷(本质就是封装了 urllib3)
注意
requests 库发送请求将网页内容下载下来以后,并不会执行 js 代码,这需要我们自己分析目标站点然后发起新的 request 请求
安装
pip3 install requests
GET 请求
requests.get(url="",
headers={'User-Agent':'',
'Referer':'',},
Cookie={},
) #headers:请求头信息,User-Agent:浏览器标识,Referer:上次请求的url
POST 请求
#GET请求
HTTP默认的请求方法就是GET
* 没有请求体
* 数据必须在1K之内!
* GET请求数据会暴露在浏览器的地址栏中
GET请求常用的操作:
1. 在浏览器的地址栏中直接给出URL,那么就一定是GET请求
2. 点击页面上的超链接也一定是GET请求
3. 提交表单时,表单默认使用GET请求,但可以设置为POST
#POST请求
(1). 数据不会出现在地址栏中
(2). 数据的大小没有上限
(3). 有请求体
(4). 请求体中如果存在中文,会使用URL编码!
#!!!requests.post()用法与requests.get()完全一致,特殊的是requests.post()有一个data参数,用来存放请求体数据
发送 post 请求,模拟浏览器的登录行为
'''
一 目标站点分析
浏览器输入https://github.com/login
然后输入错误的账号密码,抓包
发现登录行为是post提交到:https://github.com/session
而且请求头包含cookie
而且请求体包含:
commit:Sign in
utf8:?
authenticity_token:lbI8IJCwGslZS8qJPnof5e7ZkCoSoMn6jmDTsL1r/m06NLyIbw7vCrpwrFAPzHMep3Tmf/TSJVoXWrvDZaVwxQ==
login:egonlin
password:123
二 流程分析
先GET:https://github.com/login拿到初始cookie与authenticity_token
返回POST:https://github.com/session, 带上初始cookie,带上请求体(authenticity_token,用户名,密码等)
最后拿到登录cookie
ps:如果密码时密文形式,则可以先输错账号,输对密码,然后到浏览器中拿到加密后的密码,github的密码是明文
'''
import requests
import re
#第一次请求
r1=requests.get('https://github.com/login')
r1_cookie=r1.cookies.get_dict() #拿到初始cookie(未被授权)
authenticity_token=re.findall(r'name="authenticity_token".*?value="(.*?)"',r1.text)[0] #从页面中拿到CSRF TOKEN
#第二次请求:带着初始cookie和TOKEN发送POST请求给登录页面,带上账号密码
data={
'commit':'Sign in',
'utf8':'?',
'authenticity_token':authenticity_token,
'login':'xxxxxx',
'password':'xxxxx'
}
r2=requests.post('https://github.com/session',
data=data,
cookies=r1_cookie
)
login_cookie=r2.cookies.get_dict()
#第三次请求:以后的登录,拿着login_cookie就可以,比如访问一些个人配置
r3=requests.get('https://github.com/settings/emails',
cookies=login_cookie)
print('xxxx' in r3.text) #True
自动登陆github(自己处理cookie)
来源: http://www.bubuko.com/infodetail-2467992.html