ITPub博客

首页 > IT职业 > IT职场 > python爬虫如何使用request库处理cookie?

python爬虫如何使用request库处理cookie?

IT职场 作者:darkfiber 时间:2021-09-11 09:59:53 0 删除 编辑

python爬虫中使用urli库可以使用opener"发送多个请求,这些请求是能共享处理cookie的,小编之前也提过python爬虫中使用request库会比urllib库更加⽅便,使用使用requests也能达到共享cookie的目的,即使用request库get方法和使用requests库提供的session对象都可以处理。

方法一:使用request库get方法

 
resp = requests.get('http://www.baidu.com/') 
print(resp.cookies) 
print(resp.cookies.get_dict())

方法二:使用requests库提供的 session对象

import requests
 
url = "http://www.renren.com/PLogin.do" 
data = {"email":"970138074@qq.com",'password':"pythonspider"} 
headers = { 'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 
(KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36" }
#登录
session = requests.session() session.post(url,data=data,headers=headers)
#访问
resp = session.get('http://www.renren.com/256450404/profile')
print(resp.text) ```

以上就是python爬虫使用request库处理cookie的两种方法,洗碗能对你有所帮助哟~更多python爬虫学习推荐:python爬虫入门教程。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4399/viewspace-2830740/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-12

  • 博文量
    93
  • 访问量
    527