您的位置:首页 > 编程语言 > Python开发

使用python脚本模拟登录知乎

2017-03-02 15:13 656 查看

python登录知乎—无验证码

import requests

from bs4 import BeautifulSoup

class ZHIHU():

def __init__(self):
self.login_url = 'https://www.zhihu.com/login/phone_num'

self.login_data = {'phone_num':'xxxxxxxxxxx',
'password':'xxxxxxxx'}

self.headers = {'Host':'www.zhihu.com',
'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}

def login_in(self):
session = requests.session()
response1 = requests.get('https://www.zhihu.com/', headers=self.headers)
soup1 = BeautifulSoup(response1.text, 'html.parser')
xsrf = soup1.find('input',attrs={'name':'_xsrf'})['value']
self.login_data['_xsrf']=xsrf

response2 = session.post(self.login_url, data=self.login_data, headers=self.headers)
print(response2.text)
return session


zhihu_spider = ZHIHU()

zhihu_spider.login()

登录成功(r=0),输出如下内容:

{“r”:0,

“msg”: “\u767b\u5f55\u6210\u529f”

}

登录失败(r=1),如密码错误时输出如下内容:

{

“r”: 1,

“errcode”: 100005,

"data": {"password":"\u5e10\u53f7\u6216\u5bc6\u7801\u9519\u8bef"},

"msg": "\u5e10\u53f7\u6216\u5bc6\u7801\u9519\u8bef"


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: