request - cookie操作(二)
2024-09-01 00:22:11
from urllib import request,parse
from http.cookiejar import CookieJar headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36"
} def get_opener():
# 1.登录
# 1.1 创建一个cookiejar对象
cookiejar = CookieJar()
# 1.2 使用cookiejar创建一个HTTPCookieProcessor对象
handler = request.HTTPCookieProcessor(cookiejar)
# 1.3 使用上一步的handler创建一个opener
opener = request.build_opener(handler)
return opener def login_renern(opener):
# 1.4 使用opener发送登录的请求(输入账号和密码)
data = {
"email":"xxxx",
"password":"xxxx"
}
login_url = "http://www.renren.com/PLogin.do"
req = request.Request(login_url,data=parse.urlencode(data).encode("utf-8"),headers=headers)
opener.open(req) def visit_profile(opener):
# 2.访问主页
pro_url = "http://www.renren.com/452057374/profile?ref=page"
#获取个人主页的页面的时候,不要新建一个opener
#而应该使用之前的那个opener,因为之前的那个opener已经包含了登录所需要的cookie信息
resp = opener.open(pro_url)
with open("renren.html","w",encoding="utf-8") as file:
file.write(resp.read().decode("utf-8")) if __name__ == '__main__':
opener = get_opener()
login_renern(opener)
visit_profile(opener)
最新文章
- 设置html title标题左侧的小图标
- python 正则
- dateset添加一列
- 解决MVC EF Code First错误:Model compatibility cannot be checked because the EdmMetadata type was not included in the model.
- android launchmode(四种启动模式)应用场景及实例
- [转]response.getWriter().write()与out.print()的区别
- Chapter 5. Label and Entry Widgets 标签和输入部件
- removeCss
- poj 2288 Islands and Bridges
- Sqlserver2012 评估期已过解决问题
- 持续集成 windows下jenkins常见问题填坑
- Eclipse 注释
- [SinGuLaRiTy] SplayTree 伸展树
- Mybatis了解(配置)
- listbox控件使用
- NDK配置debug环境时:Error:FAILURE: Build failed with an exception
- PHP多个一维数组合并成二维数组的简易方法
- 自定义Java注解的方式与应用
- junit4实验报告
- xtrabackup备份MySQL并主从同步
热门文章
- VS2015静态编译libcurl(C++ curl封装类)
- 帕斯瓦尔定理(Parseval's theorem)
- vs2013+cocos2d-x-2.2.3组态
- EJB什么,它真的有这么神奇??
- 关闭Mac OS 的Rootless
- 简化连接Buffer对象的过程
- 自动备份Mysql数据库脚本
- ApplicationCommands用于表示应用程序程序员经常遇到的常见命令,类似于ctrl+c
- vs2015 cordova环境安装
- 财富500强的前10个公司里有8个公司在使用Qt(Qt自己认为的优点是:直觉主义、跨平台、节省时间),以及一些商业案例