代码github地址:zhihu
首先分析知乎登录页,一般模拟登录走的都是手机页,比较方便
为了得到验证码我特意都输错了几次密码,知乎的登录不一定有验证码,所以我们在代码实现的过程中需要做判断是否需要验证码
引入依赖:
from urllib import request,parse
from bs4 import BeautifulSoup
import http.cookiejar
import json
import random
import time
import configparser
构建全局请求头
因为要模拟手机,所以这里我们用的是手机的user-Agent
def build_opener():
cookie = http.cookiejar.CookieJar()
cookie_processor = request.HTTPCookieProcessor(cookie)
opener = request.build_opener(cookie_processor)
opener.addheaders = [("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1"),
("Referer", "https://www.zhihu.com/"),
("Origin", "https://www.zhihu.com/"),
("Host", "www.zhihu.com")]
request.install_opener(opener)
登录过程
def login(code=0):
login_data = configparser.ConfigParser()
login_data.read("user.ini") #将用户名密码放在user.ini配置文件
username = login_data.get("LoginInfo", "email")
password = login_data.get("LoginInfo", "password")
url = 'https://www.zhihu.com/signin'
login_url = 'https://www.zhihu.com/login/email'
captcha_url = 'https://www.zhihu.com/captcha.gif'
req = request.Request(url)
res = request.urlopen(req)
html = res.read().decode('utf-8')
soup = BeautifulSoup(html)
inputs = soup.find_all('input')
_xsrf = inputs[0]['value']
# 构造登录参数
params = {
'email': username,
'password': password,
'_xsrf': _xsrf
}
#如果code是1,说明需要验证码,读取验证码并写入到本地,然后手动输入验证码
if code == 1:
cap_parms = parse.urlencode({"r": time.time(), "type": "login"}).encode('utf-8')
captcha_req = request.Request(captcha_url,cap_parms,method="GET")
captcha_res = request.urlopen(captcha_req)
fo = open('captcha.jpg','wb+')
fo.write(captcha_res.read())
fo.close()
captcha = input("请输入验证码:\n")
params['captcha'] = captcha
params = parse.urlencode(params).encode('utf-8')
req = request.Request(login_url,params,method="POST")
res = request.urlopen(req)
result = res.read().decode('utf-8')
login_result = json.loads(result)
if login_result['r'] == 0:
print('登陆成功')
else:
if login_result['errcode'] == 1991829:
login(1)
else:
print(login_result['msg'])
login()
最后
if __name__ == '__main__':
build_opener()
login()
实例效果
代码地址:https://github.com/vincenth520/Spider/tree/master/zhihu