知乎的倒立文字驗證碼
# 登錄知乎,通過保存驗證圖片方式
import urllib.request
import urllib.parse
import time
import http.cookiejar
webUrl = "https://www.zhihu.com/login/email"#不能寫https://www.zhihu.com/#signin因為不支持重定向
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 = {
'email': '在這里寫你的賬號',
'captcha_type': 'cn',
'password': '在這里寫你的密碼',
'_xsrf': '',
'captcha': ''
}
localStorePath = "寫你想保存的驗證碼圖片的地址"
if __name__ == '__main__':
#聲明一個CookieJar對象實例來保存cookie
cookie = http.cookiejar.CookieJar()
#創建opener
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=cn' % (time.time() * 1000)
# captcha_url = 'http://www.zhihu.com/captcha.gif?r=%d&type=login' % (time.time() * 1000)#這樣獲得的是“字母+數字驗證碼”
#這個獲取驗證碼圖片的方法是不行的!
# urllib.request.urlretrieve(captcha_url, localStorePath + 'myCaptcha.gif')
#用urlopen函數保存驗證圖片
req = urllib.request.Request(url=captcha_url,headers=webheader)
content = urllib.request.urlopen(req)
# content = opener.open(req)
captcha_name = 'D:/Python學習/crawler_learning/知乎登錄專題研究/知乎驗證碼圖片/myNewCaptcha.gif'
content = content.read()
with open(captcha_name, 'wb') as f:
f.write(content)
postData['captcha'] = input('請輸入驗證碼')
# 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("D:/知乎服務器反饋的內容.txt",mode='w',encoding='utf-8') as dataFile:
dataFile.write(data)
參考:
http://blog.csdn.net/hudeyu777/article/details/76706007
http://www.lxweimin.com/p/50c5815bb60b#