開發(fā)工具
PyCharm
需要安裝的包
python放接、Selenium雁竞、requests
先來一個簡單的打開百度搜索的例子
from selenium import webdriver
class OpenBaidu:
def __init__(self, search, openurl):
self.search = search
self.openurl = openurl
def open(self):
# 初始化瀏覽器對象
option = webdriver.ChromeOptions()
option.add_experimental_option('useAutomationExtension', False)
option.add_experimental_option('excludeSwitches', ['enable-automation'])
# 不自動關(guān)閉瀏覽器
option.add_experimental_option("detach", True)
driver = webdriver.Chrome(options=option)
# 設(shè)置瀏覽器長寬
driver.set_window_size(1200, 900)
# 打開頁面
driver.get(self.openurl)
driver.find_element_by_id('kw').send_keys(self.search)
driver.find_element_by_id('su').click()
self.driver = driver
def __call__(self):
self.open()
if __name__ == '__main__':
words = '北京時間'
openUrl = 'http://www.baidu.com'
OpenBaidu(words, openUrl)()
上面的代碼主要用到的方法:
browser = webdriver.Chrome()
browser.get(url)
//初始化瀏覽器對象 并打開對應(yīng)url
browser .find_element_by_id('kw').send_keys(self.search)
browser .find_element_by_id('su').click()
//輸入搜索關(guān)鍵字并點擊查詢按鈕
對輸入框的操作可參考鏈接:
Selenium文檔:
首次運行的時候可能會報如下錯誤:
selenium.common.exceptions.WebDriverException: Message: ‘chromedriver’
參考鏈接
主要是相應(yīng)瀏覽器的驅(qū)動版本不匹配導(dǎo)致的
打開自己電腦的瀏覽器(演示google瀏覽器)在地址欄輸入chrome://version/
便可以查看到谷歌當前的版本號
接著我們來到谷歌瀏覽器驅(qū)動的下載網(wǎng)址http://chromedriver.storage.googleapis.com/index.html
下載對應(yīng)版本的驅(qū)動,解壓后就是chromedriver.exe
復(fù)制粘貼到以下兩個目錄:
C:\Program Files (x86)\Google\Chrome\Application
C:\Users\DHAdmin\PycharmProjects\pythonProject\venv\Scripts
--python安裝目錄
不知道python安裝目錄的可運行如下命令查看:
import sys
sys.path
本次案例:給惠網(wǎng)登錄并簽到
由于登錄有個圖片的驗證碼襟士,需要把圖片保存到本地以后用OCR進行識別處理览爵。
這里案例用到了百度文字識別OCR的API
用到的API網(wǎng)絡(luò)圖片文字識別
百度文字識別OCR文檔
目前大多數(shù)API都有可以免費使用的次數(shù),注冊過程就省略了 段磨。
操作步驟解析
- 初始化瀏覽器對象并打開需要登錄簽到的頁面
- 輸入用戶名和密碼
- 獲取驗證碼圖片對圖片進行識別取逾,獲取文字
- 輸入識別出的文字點擊登錄按鈕
- 登錄成功后點擊簽到
第一步的步驟基本跟打開百度搜索類似
# 初始化瀏覽器對象
option = webdriver.ChromeOptions()
option.add_experimental_option('useAutomationExtension', False)
option.add_experimental_option('excludeSwitches', ['enable-automation'])
# 不自動關(guān)閉瀏覽器
option.add_experimental_option("detach", True)
driver = webdriver.Chrome(options=option)
# 登陸頁面
login_url = "https://www.geihui.com/b/daily_attendance.php"
# 設(shè)置瀏覽器長寬--窗口最大化
driver.maximize_window()
# 打開登陸頁面
driver.get(login_url)
打開以后點擊登錄按鈕,如果未登錄會跳出登錄的彈窗,然后進行登錄的操作
# 點擊簽到按鈕
driver.find_element_by_class_name('module-btn').click()
# 輸入用戶名密碼
driver.find_element_by_name('username').send_keys(self.username)
driver.find_element_by_name('password').send_keys(self.password)
獲取驗證碼圖片保存到本地
# 截取整個頁面
driver.get_screenshot_as_file("test.png")
driver.save_screenshot("test.png")
# 找到搜索框
imgposition = driver.find_element_by_id("n_code_img")
# 截取搜索框元素
imgposition.screenshot("img.png")
接下來就是對圖片進行識別了苹支,由于識別圖片需要帶token 所以要先獲取百度文字識別OCR的token
# 放一個官網(wǎng)提供的請求token的示例
# encoding:utf-8
import requests
# client_id 為官網(wǎng)獲取的AK砾隅, client_secret 為官網(wǎng)獲取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【官網(wǎng)獲取的AK】&client_secret=【官網(wǎng)獲取的SK】'
response = requests.get(host)
if response:
print(response.json())
文字識別的API有好多,要用哪種可以自己參考文檔選擇-這里用到的是網(wǎng)絡(luò)圖片文字識別债蜜,每天有500次免費調(diào)用的次數(shù)晴埂,后期是否會收費未知。還是放一個官網(wǎng)調(diào)用的示例(如果是點選驗證碼的話需要返回位置
)
# encoding:utf-8
import requests
import base64
'''
網(wǎng)絡(luò)圖片文字識別
'''
request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/webimage"
# 二進制方式打開圖片文件
f = open('[本地文件]', 'rb')
img = base64.b64encode(f.read())
params = {"image":img}
access_token = '[調(diào)用鑒權(quán)接口獲取的token]'
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
print (response.json())
有了識別結(jié)果以后寻定,只要獲取到文字進行輸入儒洛,再點擊登錄,到此已經(jīng)完成了70%
了狼速。由于文字識別并不是百分百有返回結(jié)果的琅锻,所以我們需要加上一個判斷,如果沒有識別出來的時候唐含,點擊刷新驗證碼按鈕浅浮,,重新對圖片進行保存然后識別捷枯。
if response:
if len(response.json().get('words_result')) > 0:
# 輸入驗證碼文字
self.driver.find_element_by_id('top_loginvc').send_keys(response.json().get('words_result')[0].get('words'))
# 點擊登錄
self.driver.find_element_by_xpath('//*[@id="uloginfo"]/div[4]/input').click()
time.sleep(3)
# 點擊簽到
try:
self.driver.find_element_by_class_name('module-btn').click()
# print(response.json())
# return response.json().get('words_result')
except:
self.refreshcode()
else:
self.refreshcode()
def refreshcode(self):
# 點擊刷新驗證碼
self.driver.find_element_by_css_selector('.refresh.pngBase').click()
time.sleep(3)
# 保存新的驗證碼圖片覆蓋原來的
# 截取整個頁面
self.driver.get_screenshot_as_file("test.png")
self.driver.save_screenshot("test.png")
# 找到搜索框
imgposition = self.driver.find_element_by_id("n_code_img")
# 截取搜索框元素
imgposition.screenshot("img.png")
# 重新登陸
self.ocr_b64()
完整代碼
import time
import requests
from selenium import webdriver
import base64
APP_ID = 'APP_ID' # 填寫你的API Key
SECRET_KEY = 'SECRET_KEY' # 填寫你的Secret Key
TOKEN_URL = 'https://aip.baidubce.com/oauth/2.0/token' # 獲取token請求url
OCR_URL = 'https://aip.baidubce.com/rest/2.0/ocr/v1/webimage' # 文字識別OCRAPI
class Check:
def __init__(self, username, password):
self.username = username
self.password = password
def check(self):
# 初始化瀏覽器對象
option = webdriver.ChromeOptions()
option.add_experimental_option('useAutomationExtension', False)
option.add_experimental_option('excludeSwitches', ['enable-automation'])
# 不自動關(guān)閉瀏覽器
option.add_experimental_option("detach", True)
driver = webdriver.Chrome(options=option)
# 登陸頁面
login_url = "https://www.geihui.com/b/daily_attendance.php"
# 設(shè)置瀏覽器長寬
driver.maximize_window()
# driver.set_window_size(1200, 900)
# 打開登陸頁面
driver.get(login_url)
# 點擊簽到按鈕
driver.find_element_by_class_name('module-btn').click()
# 輸入用戶名密碼
driver.find_element_by_name('username').send_keys(self.username)
driver.find_element_by_name('password').send_keys(self.password)
# 截取整個頁面
driver.get_screenshot_as_file("test.png")
driver.save_screenshot("test.png")
# 找到搜索框
imgposition = driver.find_element_by_id("n_code_img")
# 截取搜索框元素
imgposition.screenshot("img.png")
self.driver = driver
def fetch_token(self):
data = {
'grant_type': 'client_credentials',
'client_id': APP_ID,
'client_secret': SECRET_KEY
}
r = requests.post(TOKEN_URL, data=data)
if 'access_token' in r.json():
# print(r.json())
return r.json().get('access_token')
else:
print('請檢查獲取access_token的URL, APP_ID, SECRET_KEY!')
def ocr_b64(self):
'''
傳入base64編碼格式的圖片數(shù)據(jù)滚秩,識別圖片中的文字
:params: base64編碼格式的圖片數(shù)據(jù)
:return: 返回識別后的文字字符串
'''
access_token = self.fetch_token() # 獲取 token
# 打開本地圖片
f = open('./img.png', 'rb')
img = base64.b64encode(f.read())
if access_token:
params = {"image": img}
access_token = access_token
request_url = OCR_URL + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
if len(response.json().get('words_result')) > 0:
# 輸入驗證碼文字
self.driver.find_element_by_id('top_loginvc').send_keys(response.json().get('words_result')[0].get('words'))
# 點擊登錄
self.driver.find_element_by_xpath('//*[@id="uloginfo"]/div[4]/input').click()
time.sleep(3)
# 點擊簽到
try:
self.driver.find_element_by_class_name('module-btn').click()
# print(response.json())
# return response.json().get('words_result')
except:
self.refreshcode()
else:
self.refreshcode()
def refreshcode(self):
# 點擊刷新驗證碼
self.driver.find_element_by_css_selector('.refresh.pngBase').click()
time.sleep(3)
# 保存新的驗證碼圖片覆蓋原來的
# 截取整個頁面
self.driver.get_screenshot_as_file("test.png")
self.driver.save_screenshot("test.png")
# 找到搜索框
imgposition = self.driver.find_element_by_id("n_code_img")
# 截取搜索框元素
imgposition.screenshot("img.png")
# 重新登陸
self.ocr_b64()
def __call__(self):
self.check()
time.sleep(3)
self.fetch_token()
time.sleep(3)
self.ocr_b64()
if __name__ == '__main__':
# 用戶名和密碼
username = 'xxxxx'
password = 'xxxxx'
Check(username, password)()
第一次寫代碼還可進行完善,有興趣的可以拿去改改試試