Google 使用 Selenium Python 搜索无效
Google search with Selenium Python doesn't work
我想使用 Selenium Python 从 Google 结果页面中检索 phone 个兴趣点。我可以接受 Google 的使用条款,但我既不能输入查询也不能按下按钮。这是我的代码:
rom selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
url = 'https://www.google.it/'
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
driver.get(url)
time.sleep(0.5)
# Accept the Google terms of use
driver.execute_script('return document.querySelector("#L2AGLb > div")').click()
names = ['Ristorante Roma Antica Roma', 'Ristorante e Braceria Al Piave Roma']
for name in names:
input_search = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'gLFyf gsfi')))
input_search.clear()
input_search.send_keys(name)
time.sleep(0.5)
wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'gNO89b'))).click()
time.sleep(0.5)
# Extract the phone number here
try:
phone_number = ...
except:
pass
driver.execute_script("window.history.go(-1)")
我应该在单击“搜索”按钮后从页面右侧的面板中提取 phone 编号(如果面板可用)。
提前感谢您的建议。
附录
要找到 phone 数字:检查我必须在以下跨度标记中搜索的页面('LrzXr zdqRlf kno-fv'
是唯一的):
<span class="LrzXr zdqRlf kno-fv"><a data-dtype="d3ph" data-local-attribute="d3ph" jscontroller="LWZElb" href="#" jsdata="QKGTRc;_;B2Cx5o" jsaction="rcuQ6b:npT2md;F75qrd" data-ved="2ahUKEwik7rXAlNr2AhUFgP0HHedMA-IQkAgoAHoECDcQAw"><span><span aria-label="Chiama il numero di telefono 06 7047 6283">06 7047 6283</span></span></a></span>
和
<span class="LrzXr zdqRlf kno-fv"><a data-dtype="d3ph" data-local-attribute="d3ph" jscontroller="LWZElb" href="#" jsdata="QKGTRc;_;B4wf3Y" jsaction="rcuQ6b:npT2md;F75qrd" data-ved="2ahUKEwiwzs3blNr2AhU477sIHe5TA3sQkAgoAHoECEgQAw"><span><span aria-label="Chiama il numero di telefono 06 484467">06 484467</span></span></a></span>
我的目标是检索 '06 7047 6283'
和 '06 484467'
。
我成功地使用了正则表达式,但我想尽可能避免使用它们:
content = wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="kp-wp-tab-overview"]'))).text
phone_number = re.findall(r'Telefono: ([0-9 ]+)', content)[0]
我不确定这里的这一行是什么:
driver.execute_script('return document.querySelector("#L2AGLb > div")').click()
我在那里看不到任何与该定位器匹配的元素,它会为我抛出一个异常。
我还修复了搜索输入字段的定位器和点击 Enter
的方式,所以我的代码是:
names = ['Ristorante Roma Antica', 'Ristorante e Braceria Al Piave']
for name in names:
input_search = wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@name='q']")))
input_search.clear()
input_search.send_keys(name + Keys.ENTER)
phone = wait.until(EC.visibility_of_element_located((By.XPATH, "//span[contains(@aria-label,'Call phone number')]"))).text
print(phone)
driver.execute_script("window.history.go(-1)")
time.sleep(0.5)
而且有效。
至少它打开了搜索结果。
您的代码中有几处可以改进:
用于输入使用 google 输入名称 q
过去 5-6 年或可能更久。
可以直接发送输入,不需要显式的有#wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'gNO89b'))).click()
使用下面的 XPath:
//a[text()='Phone']/../following-sibling::span/descendant::span[@aria-label]
根据 Phone 文本提取 phone 数字。
代码:
names = ['Ristorante Roma Antica Roma', 'Ristorante e Braceria Al Piave Roma']
for name in names:
input_search = wait.until(EC.visibility_of_element_located((By.NAME, 'q')))
input_search.clear()
input_search.send_keys(name, Keys.RETURN)
time.sleep(0.5)
#wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'gNO89b'))).click()
#time.sleep(0.5)
# Extract the phone number here
try:
phone_number = wait.until(EC.visibility_of_element_located((By.XPATH, "//a[text()='Phone']/../following-sibling::span/descendant::span[@aria-label]")))
print(phone_number.text)
except:
pass
driver.execute_script("window.history.go(-1)")
进口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
输出:
+39 06 7047 6283
+39 06 484467
Process finished with exit code 0
我想使用 Selenium Python 从 Google 结果页面中检索 phone 个兴趣点。我可以接受 Google 的使用条款,但我既不能输入查询也不能按下按钮。这是我的代码:
rom selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
url = 'https://www.google.it/'
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
driver.get(url)
time.sleep(0.5)
# Accept the Google terms of use
driver.execute_script('return document.querySelector("#L2AGLb > div")').click()
names = ['Ristorante Roma Antica Roma', 'Ristorante e Braceria Al Piave Roma']
for name in names:
input_search = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'gLFyf gsfi')))
input_search.clear()
input_search.send_keys(name)
time.sleep(0.5)
wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'gNO89b'))).click()
time.sleep(0.5)
# Extract the phone number here
try:
phone_number = ...
except:
pass
driver.execute_script("window.history.go(-1)")
我应该在单击“搜索”按钮后从页面右侧的面板中提取 phone 编号(如果面板可用)。
提前感谢您的建议。
附录
要找到 phone 数字:检查我必须在以下跨度标记中搜索的页面('LrzXr zdqRlf kno-fv'
是唯一的):
<span class="LrzXr zdqRlf kno-fv"><a data-dtype="d3ph" data-local-attribute="d3ph" jscontroller="LWZElb" href="#" jsdata="QKGTRc;_;B2Cx5o" jsaction="rcuQ6b:npT2md;F75qrd" data-ved="2ahUKEwik7rXAlNr2AhUFgP0HHedMA-IQkAgoAHoECDcQAw"><span><span aria-label="Chiama il numero di telefono 06 7047 6283">06 7047 6283</span></span></a></span>
和
<span class="LrzXr zdqRlf kno-fv"><a data-dtype="d3ph" data-local-attribute="d3ph" jscontroller="LWZElb" href="#" jsdata="QKGTRc;_;B4wf3Y" jsaction="rcuQ6b:npT2md;F75qrd" data-ved="2ahUKEwiwzs3blNr2AhU477sIHe5TA3sQkAgoAHoECEgQAw"><span><span aria-label="Chiama il numero di telefono 06 484467">06 484467</span></span></a></span>
我的目标是检索 '06 7047 6283'
和 '06 484467'
。
我成功地使用了正则表达式,但我想尽可能避免使用它们:
content = wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="kp-wp-tab-overview"]'))).text
phone_number = re.findall(r'Telefono: ([0-9 ]+)', content)[0]
我不确定这里的这一行是什么:
driver.execute_script('return document.querySelector("#L2AGLb > div")').click()
我在那里看不到任何与该定位器匹配的元素,它会为我抛出一个异常。
我还修复了搜索输入字段的定位器和点击 Enter
的方式,所以我的代码是:
names = ['Ristorante Roma Antica', 'Ristorante e Braceria Al Piave']
for name in names:
input_search = wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@name='q']")))
input_search.clear()
input_search.send_keys(name + Keys.ENTER)
phone = wait.until(EC.visibility_of_element_located((By.XPATH, "//span[contains(@aria-label,'Call phone number')]"))).text
print(phone)
driver.execute_script("window.history.go(-1)")
time.sleep(0.5)
而且有效。
至少它打开了搜索结果。
您的代码中有几处可以改进:
用于输入使用 google 输入名称
q
过去 5-6 年或可能更久。可以直接发送输入,不需要显式的有
#wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'gNO89b'))).click()
使用下面的 XPath:
//a[text()='Phone']/../following-sibling::span/descendant::span[@aria-label]
根据 Phone 文本提取 phone 数字。
代码:
names = ['Ristorante Roma Antica Roma', 'Ristorante e Braceria Al Piave Roma']
for name in names:
input_search = wait.until(EC.visibility_of_element_located((By.NAME, 'q')))
input_search.clear()
input_search.send_keys(name, Keys.RETURN)
time.sleep(0.5)
#wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'gNO89b'))).click()
#time.sleep(0.5)
# Extract the phone number here
try:
phone_number = wait.until(EC.visibility_of_element_located((By.XPATH, "//a[text()='Phone']/../following-sibling::span/descendant::span[@aria-label]")))
print(phone_number.text)
except:
pass
driver.execute_script("window.history.go(-1)")
进口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
输出:
+39 06 7047 6283
+39 06 484467
Process finished with exit code 0