接口自动化requests库--带cookie的请求方式

一般携带Cookie请求有三种方式:

三种Cookie请求方式
第一种:cookie放在headers中
这里以请求我自己的博客首页为例:

首先找到登陆之后的Cookie和User-Agent,然后将User-Agent和Cookie复制到程序里面,如下:

import requests

url = "https://blog.csdn.net/williamgavin"

headers = {"User_Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36",
    "Cookie" : "这个还是算了吧,用你们自己的博客试 ^-^ "
}
response = requests.get(url, headers = headers )
with open("csdn.html", "w", encoding="utf-8") as f:
    f.write(response.content.decode());

 

 

第二种:cookie字典传给cookies参数


找到对应的cookie和User-Agent

import requests

url = "http://www.renren.com/967272361/profile"
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36",
}

# 不带上Cookie就访问不了这个页面
cookie = "anonymid=jk63khrk-y97r4p; _r01_=1; [email protected]; ln_hurl=http://hdn.xnimg.cn/photos/hdn421/20180720/1740/main_JAWQ_0aa000000ceb195a.jpg; _ga=GA1.2.273332130.1532825428; depovince=HUN; JSESSIONID=abcE5k0CiAJDc9ESVEcuw; ick_login=026ba348-e4e9-4871-9ce3-5868b95cfdd3; first_login_flag=1; loginfrom=syshome; wp_fold=0; BAIDU_SSP_lcr=https://www.baidu.com/link?url=VRx_HKUd53I5rYWZHvrQ9VVLotqST6-jtaZDlscFYCO&wd=&eqid=e957aec400037928000000065b64fcab; ick=64518f30-9a22-47df-b3c3-4114f185c3c6; t=8fcf47068763c279eea2620b51b7a3311; societyguester=8fcf47068763c279eea2620b51b7a3311; id=967272361; xnsid=fd736c63; jebecookies=3f9a3853-3371-4857-8268-308d663ca146|||||; jebe_key=19041c4e-4d38-4dc1-bfb9-124b81afae61%7C33b1d8f602cf6dd5a9834fe6f2bf97f2%7C1533346094265%7C1%7C1533346099750"

# 将上面哪个cookie转化成字典类型
cookie_dict = {i.split("=")[0]:i.split("=")[-1] for i in cookie.split("; ")}
response = requests.get(url, headers = headers, cookies = cookie_dict)

with open("renren2.html", "w", encoding="utf-8") as f:
    f.write(response.content.decode())


第三种 先发送post请求,获取cookie,带上cookie请求登陆之后的页面
这里要用到一个seesion类,seesion 实例具有的方法和requests一样,但是 seesion具有保持功能, 就类似浏览器输入一次密码之后,会自动保留cookie

seesion = requests.seesion()
seesion.post(url, data, headers) # 服务器设置在本地的cookie会保存在本地
seesion.get(url) # 会带上之前保存在seesion中的cookie,能够请求成功
这种方法要先提交自己的账号密码,并且要找到提交的地址。那么如何找到提交地址呢?
第一种方式:找form表单的action属性


import requests

headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"}

seesion = requests.session()

# 因为人人网有from表单,所以可以直接找地址:http://www.renren.com/PLogin.do
# 如果没有的就要抓包了

post_url = "http://www.renren.com/PLogin.do"    # form表单里面直接找到的
#post_url = "http://www.renren.com/ajaxLogin/login?
# 用户名作为键, 真正的密码作为值  模拟登陆
post_data = {"email":"xxxx", "password":"xxxx"}
seesion.post(post_url, headers = headers, data = post_data)

url = "再次请求登陆的url"

response = seesion.get(url, headers = headers)

with open("renren3.html", "w", encoding="utf-8") as f:
    f.write(response.content.decode())
 

你可能感兴趣的:(接口自动化)