![知乎登录cookie验证 知乎登录cookie验证](/default/index/img?u=aHR0cHM6Ly9waWFuc2hlbi5jb20vaW1hZ2VzLzcwNy84ZThkY2FlYmQ0YzhlNDQ5ODdkYmQyMjNhNDA2NjM4My5wbmc=)
import re
import requests
import codecs
# http.cookiejar 保存cookie
import http.cookiejar as cookieJar
from fake_useragent import UserAgent
import time
'''
使用requests中的session会话加载Cookie,如果有Cookie,直接加载使用,如果没有,不加载,需要先做登录操作,将登陆成功返回的cookie保存成文件,以便下次使用
在登录过程中,需要从登录页面返回的cookie中找出_xsrf,在传递参数时将其带上,并且带上用户名和密码,有可能会出现验证码,可以使用云打码工具**,把验证码带上,就可以登陆成功了,登录成功之后,利用session.cookie.save()函数 将cookie保存到本地
在之后发起请求时,只需要将之前保存的cookie携带上即可正常访问知乎数据
'''
class ZHCookies(object):
def __init__(self,phone_num,password):
self.url = 'https://www.zhihu.com/signin'
self.headers = {
"Referer": "https://www.zhihu.com/signup?next=%2F",
"Host": "www.zhihu.com"
}
self.ua = UserAgent()
self.filename = phone_num+'.txt'
self.session = requests.Session()
self.session.cookies = cookieJar.LWPCookieJar(filename=self.filename)
self.phone_num = phone_num
self.password = password
def get_cookies(self):
self.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0'
response = self.session.get(self.url,headers=self.headers)
# 查找_xsrf
res = response.headers['Set-Cookie']
xsrf = re.search(re.compile('_xsrf=(.*?);'),res)
self.login(xsrf.group(1))
def login(self,xsrf):
# 准备登录
url = 'https://www.zhihu.com/login/phone_num'
data = {
'_xsrf': xsrf,
'phone_num': self.phone_num,
'password': self.password
}
# 发送登录请求
response = self.session.post(url, data=data, headers=self.headers)
# 返回json类型的数据
res = response.json()
if res.get('r') == 0:
# 将cookie保存起来
self.session.cookies.save()
else:
print(response.json().get('msg'))
print('正在尝试重新登录.....')
time.sleep(1)
self.login(xsrf)
if __name__ == '__main__':
user_info = [
# {'phone_num':'18337152380','password':'shutun0315'},
{'phone_num':'13203726750','password':'100xintong'},
]
# for user in user_info:
# zh = ZHCookies(user['phone_num'],user['password'])
# zh.get_cookies()
print('已经获取到所有的cookies')
import random
# 随机获取一个cookie,用来发送请求
user = random.choice(user_info)
with requests.Session() as session:
# 随机加载本地的cookie文件
session.cookies = cookieJar.LWPCookieJar()
session.cookies.load(filename=user['phone_num']+'.txt')
# 发请求
response = session.get('http://www.zhihu.com',headers={
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0',
"Referer": "https://www.zhihu.com/signup?next=%2F",
"Host": "www.zhihu.com"
})
# 保存
with codecs.open('zhihu.html','w',encoding='utf-8') as f:
f.write(response.text)