#encoding=utf-8
# 登录知乎,通过保存验证图片方式
import urllib.request
import urllib.parse
import time
import http.cookiejar
webUrl = "https://www.zhihu.com/login/phone_num"#不能写https://www.zhihu.com/#signin因为不支持重定向 #如果用邮箱登录,把phone_num改成email
webheader = {
# 'Accept': 'text/html, application/xhtml+xml, */*',
# 'Accept-Language': 'zh-CN',
# 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko',
'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Mobile Safari/537.36',
# 'User-Agent': 'Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5',
# 'DNT': '1',
'Connection': 'Keep-Alive'
}
postData = {
'phone_num': '***', #如果用邮箱登录,把phone_num改成email
#'captcha_type': 'cn', #这里,如果要用倒立文字的验证码,把注释去掉
'password': '***',
'_xsrf': '',
'captcha': ''
}
localStorePath = "C:/Users/Administrator/Desktop/pyjiaoyi/Pro/925"
if __name__ == '__main__':
cookie = http.cookiejar.CookieJar()
handler = urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(handler)#建立opener对象,并添加头信息
urllib.request.install_opener(opener)
captcha_url = 'https://www.zhihu.com/captcha.gif?r=%d&type=login&lang=en' % (time.time() * 1000) #这样获得的是“字母+数字验证码” #一样的,如果要倒立文字,把en换成cn
# captcha_url = 'http://www.zhihu.com/captcha.gif?r=%d&type=login' % (time.time() * 1000)
req = urllib.request.Request(url=captcha_url,headers=webheader)
content = urllib.request.urlopen(req)
# content = opener.open(req)
captcha_name = 'ZiMu.gif'
content = content.read()
with open(captcha_name, 'wb') as f:
f.write(content)
postData['captcha'] = input('yanZhengMa')
# postData['_xsrf'] = get_xsrf()
postData['_xsrf'] = 'fa5ae712244bd4287e371801052003fc'
print(postData['_xsrf'])
#用urlopen函数传送数据给服务器实现登录
postData_encoded = urllib.parse.urlencode(postData).encode('utf-8')
req = urllib.request.Request(url=webUrl,data=postData_encoded,headers=webheader)
webPage = urllib.request.urlopen(req)
# webPage = opener.open(req)
data = webPage.read().decode('utf-8')
print(data)
with open("quqiu.txt",mode='w',encoding='utf-8') as dataFile:
dataFile.write(data)
url='https://www.zhihu.com/#signin'
filename='ZhiHucookie.txt'
cookie=http.cookiejar.MozillaCookieJar(filename)
handler=urllib.request.HTTPCookieProcessor(cookie)
opener=urllib.request.build_opener(handler)
response=opener.open(url)
cookie.save(ignore_discard=True,ignore_expires=True) #OK,登录成功; ,cookie也保存了
print(response.read().decode('utf-8'))
#{"img_size":[200,44],"input_points":[ 图片--倒立文字验证
#[12.95,14.969999999999998],[36.1,16.009999999999998],
#[57.16,24.44],[84.52,19.17],[108.72,28.64],
#[132.95,24.44],[151.89,23.380000000000002]]}
看看返回信息,我也是忙了一下午才搞出来,一开始也是各种问题,后台抓包,才知道请求的url不对,因为我用的是手机号登录,而且里面的key手机号(邮箱)有关。把打印出的文字,放在Unicode转成中文,如果key值不对的话,返回信息的,登录频繁,请稍后重试,如果验证码不对的话,返回信息是请输入正确验证码,如果都对的话,返回信息是登录成功。cookie登录还没做