欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python利用Selenium實現(xiàn)簡單的中英互譯功能

 更新時間:2024年08月27日 09:31:45   作者:aaaaac_  
Selenium 是一個用于 Web 應(yīng)用程序測試的工具,最初是為網(wǎng)站自動化測試而開發(fā)的,可以直接運行在瀏覽器上,是 Python 的一個第三方庫,對外提供的接口能夠操作瀏覽器,從而讓瀏覽器完成自動化的操作,本文介紹了如何利用Python中的Selenium實現(xiàn)簡單的中英互譯

1.環(huán)境配置 

安裝Chrome瀏覽器,并在 “關(guān)于 Chrome” 界面獲取版本信息

6b4aac24f2ab4529af3df4643192646a.png

下載與瀏覽器版本號相對應(yīng)的Chromedriver插件(點擊跳轉(zhuǎn)至下載界面),以“128.0.6613.84”的版本示例,實際上只需要標(biāo)紅的前3位版本號與瀏覽器相對應(yīng)即可

f5642723079b40598d445fe1ef4d49b1.png

點擊進(jìn)去后選擇與自己的電腦系統(tǒng)相對于的版本下載即可

fc023c02ae3648d9bd5083afe8f75bc8.png

安裝Selenium

pip install selenium

2.具體實現(xiàn)

導(dǎo)入Selenium包

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from time import sleep

指定Chrome瀏覽器的絕對路徑  (單引號內(nèi)路徑自行替換)

option.binary_location = '.\\Google\\Chrome\\Application\\chrome.exe'

創(chuàng)建 WebDriver 對象,指定ChromeDriver插件的路徑,同時運行Chrome瀏覽器  (單引號內(nèi)路徑自行替換)

wd = webdriver.Chrome(service=Service(r'.\chromedriver.exe'))

調(diào)用 WebDriver 對象的get方法讓瀏覽器打開百度翻譯的網(wǎng)頁

wd.get('https://fanyi.baidu.com/mtpe-individual/multimodal')

這時,需要我們手動在瀏覽器中打開百度翻譯網(wǎng)頁,通過審查元素的方式分別獲取到輸入?yún)^(qū)域和輸出區(qū)域的Class值,具體操作見視頻(點擊跳轉(zhuǎn)至視頻)

輸入?yún)^(qū)域:kXQpwTof

輸出區(qū)域:u4heFBcZ

讀取用戶輸入的需要翻譯的內(nèi)容,再利用Class值獲取輸入?yún)^(qū)域,將該內(nèi)容發(fā)送到輸入?yún)^(qū)域中

#讀取用戶輸入的需要翻譯的內(nèi)容
input_txt = input()
#利用Class值獲取輸入?yún)^(qū)域
input1 = wd.find_element(By.CLASS_NAME, "kXQpwTof")
#將需要翻譯的內(nèi)容發(fā)送到輸入?yún)^(qū)域中
input1.send_keys(f"{input_txt}")

利用Class值獲取輸出區(qū)域,并將輸出區(qū)域中翻譯好的文本打印到終端中

#利用Class值獲取輸出區(qū)域
output1 = wd.find_element(By.CLASS_NAME, "u4heFBcZ")
#將翻譯好的內(nèi)容打印出來
print(output1.text)

3.最終代碼

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from time import sleep
 
#隱藏瀏覽器界面
option = webdriver.ChromeOptions()
option.add_argument('--headless')
 
option.binary_location = '.\\Google\\Chrome\\Application\\chrome.exe'
wd = webdriver.Chrome(service=Service(r'.\chromedriver.exe'), options=option)
 
#以防瀏覽器還未打開就執(zhí)行打開百度翻譯網(wǎng)頁的代碼從而出現(xiàn)錯誤,這里停頓1s
sleep(1)
 
#提示一次瀏覽器已經(jīng)加載好了可以開始輸入了
print("程序加載完成!\n")
 
#設(shè)置個循環(huán),多次反復(fù)翻譯
while 1:
    #將跳轉(zhuǎn)頁面的代碼放在循環(huán)中,每次翻譯完后重新加載頁面,清空上一次的內(nèi)容
    wd.get('https://fanyi.baidu.com/mtpe-individual/multimodal')
    
    #讀取用戶輸入的需要翻譯的內(nèi)容
    input_txt = input()
    #利用Class值獲取輸入?yún)^(qū)域
    input1 = wd.find_element(By.CLASS_NAME, "kXQpwTof")
    #將需要翻譯的內(nèi)容發(fā)送到輸入?yún)^(qū)域中
    input1.send_keys(f"{input_txt}")
 
    #等待1.5s,防止還未翻譯完成就開始讀取輸出區(qū)域的內(nèi)容從而輸出空白內(nèi)容
    sleep(1.5)
    
    #利用Class值獲取輸出區(qū)域
    output1 = wd.find_element(By.CLASS_NAME, "u4heFBcZ")
    #將翻譯好的內(nèi)容打印出來
    print(output1.text)
 
    #打印分割線,起個美觀的作用
    #print('-' * 50)

4.效果展示

d6b63ffde32a4e5b9bff985859182274.png

以上就是Python利用Selenium實現(xiàn)簡單的中英互譯功能的詳細(xì)內(nèi)容,更多關(guān)于Python Selenium中英互譯的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論