http协议为无连接协议,cookie: 存放在客户端浏览器,session: 存放在Web服务器
人人网登录案例
方法一:登录网站手动抓取Cookie
1、先登录成功1次,获取到携带登陆信息的Cookie
登录成功 - 个人主页(http://www.renren.com/971989504/profile) - F12抓包 - 刷新个人主页 - 找到主页的包(home)
一般cookie都在all--> home 数据包中,
2、携带着cookie发请求
复制代码
import requests
class RenRenLogin(object):
def __init__(self):
# url为需要登录才能正常访问的地址
self.url = 'http://www.renren.com/967469305/profile'
# headers中的cookie为登录成功后抓取到的cookie
self.headers = {
# 此处注意cookie,要自己抓取
"Cookie": "xxx",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
}
# 获取个人主页响应
def get_html(self):
html = requests.get(url=self.url,headers=self.headers,verify=False).text
print(html)
self.parse_html(html)
# 可获取并解析整个人人网内需要登录才能访问的地址
def parse_html(self,html):
pass
if __name__ == '__main__':
spider = RenRenLogin()
spider.get_