(转)request模拟知乎登录(无验证码机制
原文:http://www.itnose.net/detail/6755805.html
import request
try:
import cookielib #python2版本
except:
import http.cookiejar as cookielib #python3版本
import re
import
session=request.session()
session.cookies=cookielib.LWPCookieJar(filename="cookies.txt") #将cookies存储到本地文件
#加载cookies文件
try:
session.cookies.load(ignore_discard=True)
except:
print("cookies未能加载")
User_Agent="Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36"
header={
"HOST" : "www.zhihu.com",
"Referer" : "https://www.zhihu.com",
"User_Agent" : User_Agen"
}
#获取xsrf
def get_xsrf():
response=session.post("https://www.zhihu.com",headers=header) #请求网页需带上头文件
match_obj=re.match(' .*name="_xsrf" value="(.*?)" ') #注意使用单双引号
if match_obj:
return (match_obj(1))
else:
return " "
def get_index():
response=session.get("https://www.zhihu.com",headers=header)
with open("index_page.heml",wb) as f:
f,write(response.text.encode("utf-8"))
print ("ok")
#模拟知乎登录
def zhihu_login(account,password):
if re.match("^1\d{10}",account): #验证账号是否为手机号
print ("手机登录")
post_url="https://www.zhihu.com/login/phone_num"
post_data={
"_xsrf" : get_xsrf(),
"phone_num" : account,
"password" : password
}
else:
if "@" in account:
print (“邮箱登录”)
post_url="https://www.zhihu.com/login/email"
post_data={
"_xsrf" : get_xsrf(),
"email" : account,
"password" : password
}
response_text=session.post(post_url,post_data,headers=header)
session.cookies.save()
#验证是否登录成功
def is_login():
inbox_url="https://www.zhihu.com/inbox"
response=session.get(inbox_url,headers=header,allow_redirects=False)
if response.status_code !=200:
return False
else:
return True
zhihu.login("18782902568","admin123")
get_index()
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性