# _*_ coding: utf-8 _*_
'''
模擬知乎登陸并使用tesseract-ocr識別驗證碼
Author: Insomnia
Version: 0.0.1
Date: 2017-09-07
Language: Python3.6.2
Editor: Sublime Text3
'''
import requests, os, time, re
from bs4 import BeautifulSoup
from PIL import Image
# 當前項目路徑
cur_path = os.getcwd() + '/'
class ZhiHuSpider(object):
'''
本類主要用于實現模擬知乎登陸并使用tesseract-ocr識別驗證碼
Attribute:
session: 建立會話
url_signin: 登陸頁面鏈接
url_login: 登陸接口
url_captcha: 驗證碼鏈接
headers: 請求頭部信息
num: 識別驗證碼次數
'''
def __init__(self):
self.session = requests.Session();
self.url_signin = 'https://www.zhihu.com/#signin'
self.url_login = 'https://www.zhihu.com/login/email'
self.url_captcha = 'https://www.zhihu.com/captcha.gif?r=%d&type=login' % (time.time() * 1000)
self.headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36'}
self.num = 1
def get_captcha(self):
'''
獲取并使用tesseract-ocr識別驗證碼
Returns:
返回驗證碼
'''
# 創建文件夾存放驗證碼
if not os.path.exists(cur_path + 'captcha'):
os.mkdir(cur_path + 'captcha')
captcha_text = ''
while True:
# 下載驗證碼圖片
captcha = self.session.get(self.url_captcha, headers=self.headers).content
captcha_path = cur_path + 'captcha/captcha.gif' # 驗證碼圖片路徑
captcha_path_new = cur_path + 'captcha/captcha_new.gif' # 處理后的驗證碼圖片路徑
with open(captcha_path, 'wb') as f:
f.write(captcha)
# 圖片處理便于識別文字:彩色轉灰度,灰度轉二值,二值圖像識別
im = Image.open(captcha_path)
w, h = im.size
im = im.convert('L') # convert()用于不同模式圖像之間的轉換
threshold = 100 # 圖片降噪處理
table = []
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(1)
im = im.point(table, '1')
w_new, h_new = w * 2, h * 2
im = im.resize((w_new, h_new), Image.ANTIALIAS) # 圖片放大
im.save(captcha_path_new)
# 執行tesseract-ocr識別驗證碼
captcha_text_path = cur_path + 'captcha/captcha_text' # 識別驗證碼后驗證碼文字的存放路徑
cmd = '/usr/bin/tesseract %s %s' % (captcha_path_new, captcha_text_path)
os.system(cmd)
time.sleep(2)
with open('%s.txt' % captcha_text_path, 'r') as f:
try:
captcha_text = f.read().strip()
print('第 %d 次識別的驗證碼為:%s' % (self.num, captcha_text))
regex = re.compile('^[0-9a-zA-Z]{4}$') # 正則表達式
if captcha_text and re.search(regex, captcha_text):
break;
else:
print('驗證碼無效!重新識別中...')
self.num += 1
# time.sleep(2) # 避免過于頻繁的訪問
except Exception as e:
print('Exception:', e)
break;
return captcha_text
def login(self, username, password):
'''
登陸接口
Args:
username: 登陸賬戶
password: 登陸密碼
Returns:
返回登錄結果list
'''
soup = BeautifulSoup(self.session.get(self.url_signin, headers=self.headers).content, 'html.parser')
# 獲取xsrf_token
xsrf = soup.find('input', attrs={'name': '_xsrf'}).get('value')
post_data = {
'_xsrf': xsrf,
'email': username,
'password': password,
'captcha': self.get_captcha()
}
login_ret = self.session.post(self.url_login, post_data, headers=self.headers).json()
return login_ret
def get_index_topic(self):
'''
獲取首頁第一條話題記錄
'''
print('-'*50, '獲取知乎首頁第一條話題記錄', '-'*50, sep="\n")
soup = BeautifulSoup(self.session.get(self.url_signin, headers=self.headers).content, 'html.parser')
item = soup.find('div', attrs={'class': 'Card TopstoryItem TopstoryItem--experimentFont18'})
popover = item.find('div', attrs={'class': 'Feed-title'}).find('div', attrs={'class': 'Popover'}).find('div').get_text() # 話題
print('來自話題:%s' % popover)
title = item.find('div', attrs={'class': 'ContentItem AnswerItem'}).find('meta', attrs={'itemprop': 'name'}).get('content') # 標題
print('title: %s' % title)
content = item.find('div', attrs={'class': 'ContentItem AnswerItem'}).find('span', attrs={'class': 'RichText CopyrightRichText-richText'}).get_text() # 內容
print('content: %s' % content)
if __name__ == '__main__':
zhihu = ZhiHuSpider()
# 循環嘗試登陸
while True:
ret = zhihu.login('知乎登陸郵箱', '知乎密碼')
if ret['r'] == 0:
print('登陸成功! ')
zhihu.get_index_topic()
break
else:
print('登陸失敗: %s' % ret['msg'])
運行效果圖
image.png