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)

最新文章

  1. 设置html title标题左侧的小图标
  2. python 正则
  3. dateset添加一列
  4. 解决MVC EF Code First错误:Model compatibility cannot be checked because the EdmMetadata type was not included in the model.
  5. android launchmode(四种启动模式)应用场景及实例
  6. [转]response.getWriter().write()与out.print()的区别
  7. Chapter 5. Label and Entry Widgets 标签和输入部件
  8. removeCss
  9. poj 2288 Islands and Bridges
  10. Sqlserver2012 评估期已过解决问题
  11. 持续集成 windows下jenkins常见问题填坑
  12. Eclipse 注释
  13. [SinGuLaRiTy] SplayTree 伸展树
  14. Mybatis了解(配置)
  15. listbox控件使用
  16. NDK配置debug环境时:Error:FAILURE: Build failed with an exception
  17. PHP多个一维数组合并成二维数组的简易方法
  18. 自定义Java注解的方式与应用
  19. junit4实验报告
  20. xtrabackup备份MySQL并主从同步

热门文章

  1. VS2015静态编译libcurl(C++ curl封装类)
  2. 帕斯瓦尔定理(Parseval's theorem)
  3. vs2013+cocos2d-x-2.2.3组态
  4. EJB什么,它真的有这么神奇??
  5. 关闭Mac OS 的Rootless
  6. 简化连接Buffer对象的过程
  7. 自动备份Mysql数据库脚本
  8. ApplicationCommands用于表示应用程序程序员经常遇到的常见命令,类似于ctrl+c
  9. vs2015 cordova环境安装
  10. 财富500强的前10个公司里有8个公司在使用Qt(Qt自己认为的优点是:直觉主义、跨平台、节省时间),以及一些商业案例