requests模擬登陸的三種方式

2022-11-24 21:46:50 字數 1889 閱讀 6233

登陸的方式

1.例項化session,使用session傳送post請求,在使用它獲取登陸後的頁面

# coding=utf-8

import requests

session = requests.session()

post_url = ""

post_data =

headers =

#使用session傳送post請求,cookie儲存在其中

session.post(post_url,data=post_data,headers=headers)

#在使用session進行請求登陸之後才能訪問的地址

r = session.get("",headers=headers)

#儲存頁面

with open("renren1.html","w",encoding="utf-8") as f:

f.write(r.content.decode())

2.headers中新增cookie鍵,值為cookie字串

# coding=utf-8

import requests

headers =

r = requests.get("",headers=headers)

#儲存頁面

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

f.write(r.content.decode())

3.在請求方法中新增cookies引數,接收字典形式的cookie.字典形式的cookie中的鍵是cookie的name,值是cookie的value

# coding=utf-8

import requests

headers =

cookies="anonymid=j3jxk555-nrn0wh; _r01_=1; _ga=ga1.2.1274811859.1497951251; _de=bf09ee3a28ded52e6b65f6a4705d973f1383380866d39ff5; [email protected]; depovince=bj; jebecookies=54f5d0fd-9299-4bb4-801c-eefa4fd3012b|||||; jsessionid=abci6tfwh4n4t_awjnvdw; ick_login=4be198ce-1f9c-4eab-971d-48abfda70a50; p=0cbee3304bce1ede82a56e901916d0949; first_login_flag=1; ln_hurl= t=79bdd322e760beae79c0b511b8c92a6b9; societyguester=79bdd322e760beae79c0b511b8c92a6b9; id=327550029; xnsid=2ac9a5d8; loginfrom=syshome; ch_id=10016; wp_fold=0"

cookies =

print(cookies)

r = requests.get("",headers=headers,cookies=cookies)

#儲存頁面

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

f.write(r.content.decode())

python中Requests的三種引數請求方式

url引數請求 import requests url parameters 請求方式 url引數 例如 以get 方式請求 params responds requests.get params params print responds.text print responds.url 表單引數請...