4. 尝试使用session登录人人网(别试,了解一下)
# -*- coding: utf-8 -*- import requests session = requests.session() post_url = "http://www.renren.com/PLogin.do" post_data = {"email":"mr_mao_hacker@163.com", "password":"alarmchime"} headers = { "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36" } # 使用session发送post请求,cookie保存在其中 session.post(post_url, data=post_data, headers=headers) # 在使用session进行请求登录之后才能访问 r = session.get("http://www.renren.com/327550029/profile", headers=headers) print(r.request.url) # 保存页面 with open("reren1.html", "w", encoding="utf-8") as f: f.write(r.content.decode())
现在好像要验证码了。。。。。
5. 字典推导式和列表推导式
{i.split("=")[0]:i.split("=")[1] for i in cookie.split("; ")}