一、登錄知乎
這里用的是手機(jī)端登錄的,知乎登錄的鏈接
post請(qǐng)求的參數(shù):
_xsrf:據(jù)說是防跨站請(qǐng)求的拂玻;
password:密碼
email:登錄郵箱
captcha:驗(yàn)證碼
在登錄頁(yè)定位到這些參數(shù)捏检,用post在登錄時(shí)傳入這些參數(shù),就可以登錄了壮吩;
下面是完整代碼:
import requests, time
from http import cookiejar
from PIL import Image
import re
session = requests.session()
session.cookies = cookiejar.LWPCookieJar(filename='cookies.txt')
headers = {
'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'
}
try:
# 從本地文件加載cookies
# ignore_discard的意思是即使cookies將被丟棄也將它保存下來进苍,ignore_expires的意思是如果在該文件中cookies已經(jīng)存在,則覆蓋原文件寫入
session.cookies.load(ignore_discard=True)
except Exception as e:
print('exception:', e)
print('沒有cookie信息')
def get_xsrf():
index_url = 'https://www.zhihu.com/signin?next=/'
# _xsrf 是一個(gè)動(dòng)態(tài)變化的必要參數(shù)
index_page = session.get(index_url, headers=headers)
html = index_page.text
pattern = r'name="_xsrf" value="(.*?)"'
# 這里的_xsrf 返回的是一個(gè)list
_xsrf = re.findall(pattern, html)
return _xsrf[0]
def get_captcha():
t = str(int(time.time() * 1000))
captcha_url = 'https://www.zhihu.com/captcha.gif?r=' + t + "&type=login"
response = session.get(captcha_url, headers=headers)
captcha_name = 'captcha.gif'
with open(captcha_name, 'wb') as f:
f.write(response.content)
im = Image.open(captcha_name)
im.show()
return input('請(qǐng)輸入驗(yàn)證碼: ')
def get_email():
return input('請(qǐng)輸入郵箱: ')
def get_password():
return input('請(qǐng)輸入密碼: ')
def login(email, password, _xsrf, captcha):
data = {
'_xsrf': _xsrf,
'password': password,
'email': email,
'captcha': captcha
}
login_url = 'https://www.zhihu.com/login/email'
response = session.post(login_url, data=data, headers=headers)
print('response.json() =', response.json())
# 保存cookies到本地
session.cookies.save()
def isLogin():
# 查看用戶個(gè)人信息來判斷是否已經(jīng)登錄
url = "https://www.zhihu.com/settings/profile"
# 這里重定向一定要設(shè)置為false, 否則就算沒有登錄會(huì)被重定向到登錄的地址去, 然后code就返回200了
response = session.get(url, headers=headers, allow_redirects=False)
code = response.status_code
if code == 200:
return True
else:
return False
if __name__ == '__main__':
if isLogin():
print('您已經(jīng)登錄')
else:
email = get_email()
password = get_password()
_xsrf = get_xsrf()
print('_xsrf =', _xsrf)
captcha = get_captcha()
login(email, password, _xsrf, captcha)
運(yùn)行結(jié)果:
這里是用郵箱登錄的鸭叙,驗(yàn)證碼需要手動(dòng)輸入觉啊;
二、自動(dòng)識(shí)別驗(yàn)證碼
python識(shí)別驗(yàn)證碼沈贝,需要安裝的模塊
Ubuntu版本:
1杠人、tesseract-ocr安裝
sudo apt-get install tesseract-ocr
2、pytesseract安裝
sudo pip install pytesseract
3宋下、Pillow 安裝
sudo pip install pillow
其他linux版本(如centos):
1嗡善、tesseract-ocr安裝 沒找到直接命令安裝,所以需要手動(dòng)下載安裝包学歧。 https://github.com/tesseract-ocr/tesseract 在上述地址中下載最新的tesseract-ocr的安裝包罩引,并解壓。 通過以下命令安裝:
(1)cd tesseract-3.04.01
(2)./autogen.sh
(3)./configure 注意撩满,如果出現(xiàn)error: leptonica not found蜒程,需要下載安裝leptonica http://www.leptonica.org/download.html
(4)make
(5)make install
(6)ldconfig
2绅你、pytesseract安裝 sudo pip install pytesseract
3、Pillow 安裝 sudo pip install pillow
windows版本:
1昭躺、tesseract-ocr安裝 下載忌锯,并安裝。
注意:如果是64位的用戶领炫,在安裝的時(shí)需要改變安裝目錄偶垮,如下圖所示
2、pytesseract安裝 pip install pytesseract
3帝洪、Pillow 安裝 pip install pillow
下面驗(yàn)證一下識(shí)別驗(yàn)證碼:
#coding:utf-8
import pytesseract
from PIL import Image
image = Image.open('code.png')
code = pytesseract.image_to_string(image)
print(code)
可能會(huì)遇到的問題:
問題1似舵、FileNotFoundError: [WinError 2] 系統(tǒng)找不到指定的文件。
解決辦法:
1葱峡、[推薦]: 將tesseract.exe添加到環(huán)境變量PATH中砚哗,
例如: 默認(rèn)路徑為C:\Program Files (x86)\Tesseract-OCR
注意: 為了使環(huán)境變量生效,需要關(guān)閉cmd窗口或是關(guān)閉pycharm等ide重新啟動(dòng)
2砰奕、 修改pytesseract.py文件蛛芥,指定tesseract.exe安裝路徑
打開文件 pytesseract.py,找到如下代碼军援,將tesseract_cmd的值修改為全路徑仅淑,在此使用就不會(huì)報(bào)錯(cuò)了。
# CHANGE THIS IF TESSERACT IS NOT IN YOUR PATH, OR IS NAMED DIFFERENTLY
# tesseract_cmd = 'tesseract'
tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract.exe'
3胸哥、 在實(shí)際運(yùn)行代碼中指定
pytesseract.pytesseract.tesseract_cmd = 'D:\\Tesseract-OCR\\tesseract.exe'
問題2:
pytesseract.pytesseract.TesseractError: (1, 'Error opening data file \Tesseract-OCR\tessdata/eng.traineddata')
解決方法:
1涯竟、[推薦]:將tessdata目錄的上級(jí)目錄所在路徑(默認(rèn)為tesseract-ocr安裝目錄)添加至TESSDATA_PREFIX環(huán)境變量中
例如: C:\Program Files (x86)\Tesseract-OCR
2、 在.py文件配置中指定tessdata-dir
tessdata_dir_config = '--tessdata-dir "D:\\Tesseract-OCR\\tessdata"'
# tessdata_dir_config = '--tessdata-dir "'C:\\Program Files (x86)\\Tesseract-OCR\\tessdata"'
pytesseract.image_to_string(image, config=tessdata_dir_config)
小試牛刀:
#coding:utf-8
import pytesseract
from PIL import Image
image = Image.open('code.png')
code = pytesseract.image_to_string(image)
print(code)
運(yùn)行結(jié)果: