使用硒从地图中抓取数据
Scrape Data from Map using selenium
您好,我正在尝试从 website 上的 google 地图上抓取数据 它会 运行 两次正常,但在使用硒时第三次出现错误.
这是我的代码:
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from shutil import which
import time
import pandas as pd
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
Names = []
Emails = []
chrome_path = which('chromedriver')
driver = webdriver.Chrome(executable_path=chrome_path)
driver.maximize_window()
error = []
Address = []
Debug = []
for a in range(1,72):
driver.get('https://www.boogsport.vlaanderen/zoek-een-club/')
try:
driver.find_element_by_xpath('//button[@id="oc_cb_btn"]').click()
except:
error.append("NOne")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,'//div[@class="entry-content"]/iframe')))
xpath = "//div[@aria-label='Map']/div[3]/div/div/div["
xpath += str(a)
xpath += "]"
driver.find_element_by_xpath(xpath).click()
# WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, xpath))).click()
# time.sleep(2)
name = driver.find_element_by_xpath('//div[@class="qqvbed-bN97Pc"]/div[1]/div[1]/div[2]')
Names.append(name.text)
try:
mail = driver.find_element_by_xpath('//div[@class="qqvbed-bN97Pc"]/div[1]/div[2]/div[2]/a')
Emails.append(mail.get_attribute('href'))
except:
Emails.append("None")
try:
adress = driver.find_element_by_xpath('//div[@class="qqvbed-bN97Pc"]/div[2]/div[2]')
Address.append(adress.text)
except:
Address.append("None")
df = pd.DataFrame(Names , columns = ['Name'])
df['Email'] = Emails
df['Address'] = Address
print(df)
我收到这个错误:
提高exception_class(消息、屏幕、堆栈跟踪)
selenium.common.exceptions.ElementClickInterceptedException:消息:元素点击被截获:元素 ... 在点 (616、132) 不可点击。其他元素将收到点击:
(会话信息:chrome=87.0.4280.88)
有人知道问题出在哪里吗?
没有 NoSuchElementException
就无法处理元素异常
如果第一次点击不起作用,试试这个:
from selenium.common.exceptions import NoSuchElementException
try:
element = driver.find_element_by_xpath(".//button[@id="oc_cb_btn"]")
elem.click()
except NoSuchElementException:
error.append("None")
第二次点击试试这个:
from selenium.common.exceptions import NoSuchElementException
try:
driver.find_element_by_xpath(xpath).click()
except NoSuchElementException:
pass
我希望它能奏效。让我们知道。
您好,我正在尝试从 website 上的 google 地图上抓取数据 它会 运行 两次正常,但在使用硒时第三次出现错误. 这是我的代码:
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from shutil import which
import time
import pandas as pd
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
Names = []
Emails = []
chrome_path = which('chromedriver')
driver = webdriver.Chrome(executable_path=chrome_path)
driver.maximize_window()
error = []
Address = []
Debug = []
for a in range(1,72):
driver.get('https://www.boogsport.vlaanderen/zoek-een-club/')
try:
driver.find_element_by_xpath('//button[@id="oc_cb_btn"]').click()
except:
error.append("NOne")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,'//div[@class="entry-content"]/iframe')))
xpath = "//div[@aria-label='Map']/div[3]/div/div/div["
xpath += str(a)
xpath += "]"
driver.find_element_by_xpath(xpath).click()
# WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, xpath))).click()
# time.sleep(2)
name = driver.find_element_by_xpath('//div[@class="qqvbed-bN97Pc"]/div[1]/div[1]/div[2]')
Names.append(name.text)
try:
mail = driver.find_element_by_xpath('//div[@class="qqvbed-bN97Pc"]/div[1]/div[2]/div[2]/a')
Emails.append(mail.get_attribute('href'))
except:
Emails.append("None")
try:
adress = driver.find_element_by_xpath('//div[@class="qqvbed-bN97Pc"]/div[2]/div[2]')
Address.append(adress.text)
except:
Address.append("None")
df = pd.DataFrame(Names , columns = ['Name'])
df['Email'] = Emails
df['Address'] = Address
print(df)
我收到这个错误:
提高exception_class(消息、屏幕、堆栈跟踪) selenium.common.exceptions.ElementClickInterceptedException:消息:元素点击被截获:元素 ... 在点 (616、132) 不可点击。其他元素将收到点击: (会话信息:chrome=87.0.4280.88)
有人知道问题出在哪里吗?
没有 NoSuchElementException
如果第一次点击不起作用,试试这个:
from selenium.common.exceptions import NoSuchElementException
try:
element = driver.find_element_by_xpath(".//button[@id="oc_cb_btn"]")
elem.click()
except NoSuchElementException:
error.append("None")
第二次点击试试这个:
from selenium.common.exceptions import NoSuchElementException
try:
driver.find_element_by_xpath(xpath).click()
except NoSuchElementException:
pass
我希望它能奏效。让我们知道。