大家好,我試圖從這個網站上抓取姓名和電話號碼,但它沒有點擊和復制查看電話號碼所需的“點擊顯示”元素。此外,在此之后我如何在回圈中添加多個(100 )個 url,我可以使用 bs4 實作相同的效果,因為它會更快。
from selenium import webdriver
chrome_path = r"C:\Users\lenovo\Downloads\chromedriver_win32 (5)\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("https://www.autotrader.ca/a/ram/1500/hamilton/ontario/19_12052335_/?showcpo=ShowCpo&ncse=no&ursrc=pl&urp=2&urm=8&sprx=-2")
driver.find_element_by_xpath('//p[@]').text
'2011 Ram 1500 Crew Cab Sport'
driver.find_element_by_xpath('//a[@]').click()
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <a _ngcontent-wfx-c190="" href="javascript:void(0)" class="link ng-star-inserted">...</a> is not clickable at point (1079, 593). Other element would receive the click: <div id="cookie-banner" class="container-fluid cookie-banner" style="display: block;">...</div>
uj5u.com熱心網友回復:
關于Click to Show:
您需要關閉Cookie setting彈出視窗,然后scrollIntoView單擊元素。
可以Click to Show使用以下代碼單擊:
from selenium import webdriver
import time
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(executable_path="path to chromedriver.exe")
driver.maximize_window()
driver.implicitly_wait(10)
driver.get("https://www.autotrader.ca/a/ram/1500/hamilton/ontario/19_12052335_/?showcpo=ShowCpo&ncse=no&ursrc=pl&urp=2&urm=8&sprx=-2")
wait =WebDriverWait(driver,30)
wait.until(EC.element_to_be_clickable((By.XPATH,"//button[@class='close-button']"))).click()
option = wait.until(EC.element_to_be_clickable((By.XPATH,"//a[text()= 'Click to show']")))
driver.execute_script("arguments[0].scrollIntoView(true);",option)
option.click()
time.sleep(10)
關于對多個 URL 做同樣的事情:
您可以嘗試如下:
urls = ['url1','url2']
for url in urls:
driver.get(url)
...
如果你想要這個,Beautifulsoup你需要在Beautifulsoup標簽下提出這個問題。現在您已標記python,selenium和web-scraping。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/311613.html
