python模拟登录状态_requests模拟登录

该博客介绍了如何使用Python的requests库进行模拟登录知乎的步骤,包括获取_XSRF token,判断登录状态以及保存和加载cookies。示例代码中展示了通过手机号和密码登录的流程。
摘要由CSDN通过智能技术生成

### **requests模拟登录**

~~~

import requests

try:

import cookielib

except:

import http.cookiejar as cookielib

import re

session = requests.session()

session.cookies = cookielib.LWPCookieJar(filename="cookies.txt")

try:

session.cookies.load(ignore_discard=True)

except:

print ("cookie未能加载")

agent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0"

header = {

"HOST":"www.zhihu.com",

"Referer": "https://www.zhizhu.com",

'User-Agent': agent

}

def is_login():

#通过个人中心页面返回状态码来判断是否为登录状态

inbox_url = "https://www.zhihu.com/question/56250357/answer/148534773"

response = session.get(inbox_url, headers=header, allow_redirects=False)

if response.status_code != 200:

return False

else:

return True

def get_xsrf():

#获取xsrf code

response = session.get("https://www.zhihu.com", headers=header)

match_obj = re.match('.*name="_xsrf" value="(.*?)"', response.text)

if match_obj:

return (match_obj.group(1))

else:

return ""

def get_index():

response = session.get("https://www.zhihu.com", headers=header)

with open("index_page.html", "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, data=post_data, headers=header)

session.cookies.save()

zhihu_login("18782902568", "admin123")

# get_index()

is_login()

~~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python模拟登录通常使用requests库来实现,以下是三种常见的方式: 1. 基于session的登录方式 ```python import requests # 创建session对象 s = requests.Session() # 登录接口 login_url = "http://example.com/login" # 请求参数 data = { "username": "your_username", "password": "your_password" } # 登录 s.post(login_url, data=data) # 访问需要登录才能访问的页面 response = s.get("http://example.com/profile") ``` 2. 基于cookie的登录方式 ```python import requests # 登录接口 login_url = "http://example.com/login" # 请求参数 data = { "username": "your_username", "password": "your_password" } # 登录 response = requests.post(login_url, data=data) # 获取cookie cookie_dict = response.cookies.get_dict() # 使用cookie访问需要登录才能访问的页面 response = requests.get("http://example.com/profile", cookies=cookie_dict) ``` 3. 基于token的登录方式 ```python import requests # 登录接口 login_url = "http://example.com/login" # 请求参数 data = { "username": "your_username", "password": "your_password" } # 登录 response = requests.post(login_url, data=data) # 获取token token = response.json().get("token") # 设置header中的Authorization字段 headers = { "Authorization": f"Bearer {token}" } # 使用token访问需要登录才能访问的页面 response = requests.get("http://example.com/profile", headers=headers) ``` 以上三种方式都可以实现模拟登录,具体使用哪种方式取决于登录接口的实现方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值