我的第二个爬虫,含有cookie 登录信息
import requests
#这是个含有cookie的爬虫项目
def renren_login_profile():
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3"}
#session 自动保存上次获取的cookie
session = requests.session()
#观察人人网的登陆url变化,POST请求
login_url = 'http://www.renren.com/PLogin.do'
#表单提交账号和密码
form ={
'email': '1212121211',
"password": "asdsdasd"
}
#发送post请求,记录session
session.post(login_url,data = form,headers = headers)
#目标URL ,好友页面
profile_url = 'http://www.renren.com/918315005/profile'
#使用已经保存好的cookie 发送请求
data = session.get(url=profile_url,headers=headers).content.decode()
with open('path','w') as f:
f.write(data)
if __name__ == '__main__':
renren_login_profile()