簡體   English   中英

單擊最后一個下一個按鈕時,Python +硒引發錯誤

[英]Python + selenium throws an error while clicking on the last next button

我已經用python用selenium編寫了一些代碼來解析站點的名稱。 該站點具有“下一步”按鈕以轉到其“下一頁”。 我已經嘗試過管理此程序以完美地運行我的腳本。 但是,目前我面臨兩個問題:

  1. 執行后,刮板將到達下一頁並從那里進行解析,而不會刮開起始頁,因為我無法解決邏輯問題。
  2. 如果找不到最后一個顯示為灰色的下一步按鈕,則會引發錯誤,破壞代碼。

到目前為止,這是我嘗試過的:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)

driver.get("https://www.yellowpages.com/search?search_terms=pizza&geo_location_terms=San%20Francisco%2C%20CA&page=10")

while True:
    wait.until(EC.visibility_of_element_located((By.XPATH, '//li/a[contains(@class,"next")]')))

    item = driver.find_element_by_xpath('//li/a[contains(@class,"next")]')
    if not driver.find_element_by_xpath('//li/a[contains(@class,"next")]'):
        break
    item.click()

    wait.until(EC.visibility_of_element_located((By.XPATH, '//div[@class="info"]')))

    for items in driver.find_elements_by_xpath('//div[@class="info"]'):
        name = items.find_element_by_xpath('.//span[@itemprop="name"]').text
        print(name)

driver.quit()

這是使下一個按鈕變灰的元素:

<div class="pagination"><p><span>Showing</span>361-388
of 388<span>results</span></p><ul><li><a href="/search?search_terms=pizza&amp;geo_location_terms=San%20Francisco%2C%20CA&amp;page=12" data-page="12" data-analytics="{&quot;click_id&quot;:132}" data-remote="true" class="prev ajax-page" data-impressed="1">Previous</a></li><li><a href="/search?search_terms=pizza&amp;geo_location_terms=San%20Francisco%2C%20CA&amp;page=9" data-page="9" data-analytics="{&quot;click_id&quot;:132,&quot;module&quot;:1,&quot;listing_page&quot;:9}" data-remote="true" data-impressed="1">9</a></li><li><a href="/search?search_terms=pizza&amp;geo_location_terms=San%20Francisco%2C%20CA&amp;page=10" data-page="10" data-analytics="{&quot;click_id&quot;:132,&quot;module&quot;:1,&quot;listing_page&quot;:10}" data-remote="true" data-impressed="1">10</a></li><li><a href="/search?search_terms=pizza&amp;geo_location_terms=San%20Francisco%2C%20CA&amp;page=11" data-page="11" data-analytics="{&quot;click_id&quot;:132,&quot;module&quot;:1,&quot;listing_page&quot;:11}" data-remote="true" data-impressed="1">11</a></li><li><a href="/search?search_terms=pizza&amp;geo_location_terms=San%20Francisco%2C%20CA&amp;page=12" data-page="12" data-analytics="{&quot;click_id&quot;:132,&quot;module&quot;:1,&quot;listing_page&quot;:12}" data-remote="true" data-impressed="1">12</a></li><li><span class="disabled">13</span></li></ul></div>

顯然,您應該嘗試切換抓取頁面單擊“下一步”按鈕 你也可以使用try / except避免急剎車代碼:

while True:
    # Scraping required elements first
    items = wait.until(EC.visibility_of_all_elements_located((By.XPATH, '//div[@class="info"]')))
    for item in items:
        name = item.find_element_by_xpath('.//span[@itemprop="name"]').text
        print(name)
    # ...and then try to click 'Next' button
    try:
        driver.find_element_by_xpath('//li/a[contains(@class,"next")]').click()
    except:
        break

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM