在Python中使用Selenium在 Google 中进行文本搜索
Selenium是一个强大的工具,用于通过程序控制 Web 浏览器并执行浏览器自动化。它适用于所有浏览器,适用于所有主要操作系统,其脚本是用各种语言编写的,例如Python、 Java、C# 等,我们将使用Python。
在本文中,我们将了解如何自动化浏览器。我们可以只选择单词/句子并说出搜索 并且单词/句子会自动搜索并为您提供准确的结果。
要求:
- pyautogui:PyAutoGUI 是一个面向人类的跨平台 GUI 自动化Python模块。用于以编程方式控制鼠标和键盘。
- selenium : Selenium是一个强大的工具,用于通过程序控制 Web 浏览器并执行浏览器自动化。
- Speech_recognition:语音识别是家庭自动化、人工智能等多种应用中的重要功能。
- 我们正在使用chromedriver_autoinstaller以便我们可以看到搜索词的含义。 [已在您的本地设备中安装了 Chrome 最新版本)。
循序渐进的方法:
第 1 步:导入所需模块
Python3
# import module.
# Web browser Automation
from selenium import webdriver
from time import sleep
# Support for chrome
import chromedriver_autoinstaller
# Invoking speech module
import speech_recognition as sr
# Support file for speech recognition
import pyttsx3
# Automating task
import pyautogui
Python
r = sr.Recognizer()
with sr.Microphone() as source2:
r.adjust_for_ambient_noise(source2, duration = 0.2)
audio2 = r.listen(source2)
MyText = r.recognize_google(audio2)
MyText = str(MyText.lower())
Python3
if MyText == "search":
# Automates 'copy' internally
pyautogui.hotkey('ctrl', 'c')
chrome_options = webdriver.ChromeOptions()
capabilities = {'browserName': 'chrome',
'javascriptEnabled': True}
capabilities.update(chrome_options.to_capabilities())
chromedriver_autoinstaller.install()
# Invoking the chrome
driver = webdriver.Chrome()
# Adjusting the size of the window
driver.set_window_size(1920, 1080)
driver.implicitly_wait(10)
driver.get("https://www.google.com/")
#Place where our selected word gets pasted
driver.find_element_by_xpath(
"/html/body//form[@role='search']/div[2]/div[1]//div[@class='a4bIc']/input[@role='combobox']")
.send_keys(pyautogui.hotkey('ctrl', 'v'))
Python
from selenium import webdriver
from time import sleep
import chromedriver_autoinstaller
import speech_recognition as sr
import pyttsx3
import pyautogui
while(True):
try:
r = sr.Recognizer()
with sr.Microphone() as source2:
r.adjust_for_ambient_noise(source2, duration = 0.2)
audio2 = r.listen(source2)
MyText = r.recognize_google(audio2)
MyText = str(MyText.lower())
if MyText == "search":
pyautogui.hotkey('ctrl', 'c')
chrome_options = webdriver.ChromeOptions()
capabilities = {'browserName': 'chrome', 'javascriptEnabled': True}
capabilities.update(chrome_options.to_capabilities())
chromedriver_autoinstaller.install()
driver = webdriver.Chrome()
driver.set_window_size(1920, 1080)
driver.implicitly_wait(10)
driver.get("https://www.google.com/")
driver.find_element_by_xpath(
"/html/body//form[@role='search']/div[2]/div[1]//div[@class='a4bIc']/input[@role='combobox']")
.send_keys(pyautogui.hotkey('ctrl', 'v'))
elif MyText == "stop":
break
except Exception as e:
pyautogui.press('enter')
第 2 步:让我们调用语音识别模块并启动我们的内部扬声器,以便它可以听到我们的声音作为输入并启动该过程。 MyText将我们的语音命令存储为文本。
Python
r = sr.Recognizer()
with sr.Microphone() as source2:
r.adjust_for_ambient_noise(source2, duration = 0.2)
audio2 = r.listen(source2)
MyText = r.recognize_google(audio2)
MyText = str(MyText.lower())
第 3 步:选择并说出使用您的声音进行搜索后,现在将启动该过程。使用selenium和 pyautogui 会自动获取该词并给出适当的搜索结果。
蟒蛇3
if MyText == "search":
# Automates 'copy' internally
pyautogui.hotkey('ctrl', 'c')
chrome_options = webdriver.ChromeOptions()
capabilities = {'browserName': 'chrome',
'javascriptEnabled': True}
capabilities.update(chrome_options.to_capabilities())
chromedriver_autoinstaller.install()
# Invoking the chrome
driver = webdriver.Chrome()
# Adjusting the size of the window
driver.set_window_size(1920, 1080)
driver.implicitly_wait(10)
driver.get("https://www.google.com/")
#Place where our selected word gets pasted
driver.find_element_by_xpath(
"/html/body//form[@role='search']/div[2]/div[1]//div[@class='a4bIc']/input[@role='combobox']")
.send_keys(pyautogui.hotkey('ctrl', 'v'))
下面是完整的实现:
Python
from selenium import webdriver
from time import sleep
import chromedriver_autoinstaller
import speech_recognition as sr
import pyttsx3
import pyautogui
while(True):
try:
r = sr.Recognizer()
with sr.Microphone() as source2:
r.adjust_for_ambient_noise(source2, duration = 0.2)
audio2 = r.listen(source2)
MyText = r.recognize_google(audio2)
MyText = str(MyText.lower())
if MyText == "search":
pyautogui.hotkey('ctrl', 'c')
chrome_options = webdriver.ChromeOptions()
capabilities = {'browserName': 'chrome', 'javascriptEnabled': True}
capabilities.update(chrome_options.to_capabilities())
chromedriver_autoinstaller.install()
driver = webdriver.Chrome()
driver.set_window_size(1920, 1080)
driver.implicitly_wait(10)
driver.get("https://www.google.com/")
driver.find_element_by_xpath(
"/html/body//form[@role='search']/div[2]/div[1]//div[@class='a4bIc']/input[@role='combobox']")
.send_keys(pyautogui.hotkey('ctrl', 'v'))
elif MyText == "stop":
break
except Exception as e:
pyautogui.press('enter')
演示: