【问题标题】:if or try loop for an element in a page seleniumif 或 try 循环在页面 selenium 中的元素
【发布时间】:2023-04-08 02:29:01
【问题描述】:

我正在尝试在这里抓取代理数据。我能够从第一页获取链接。我正在使用编号循环,因为我知道总页数。只要有“下一页”选项,我就尝试运行它。我尝试了“尝试”和“如果不是”,但无法弄清楚。欢迎任何帮助。这是代码。

from selenium import webdriver
import time

from selenium.common.exceptions import ElementNotVisibleException, NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
options = webdriver.ChromeOptions()
options.add_argument('headless')
driver = webdriver.Chrome('C:/Users/../Downloads/cd79/chromedriver.exe', options=options)


links_total = []
driver.get("https://www.cbp.gov/contact/find-broker-by-port?field_port_location_tid=All&field_port_code_value=")
def first_links():
    initial_data = driver.find_elements_by_tag_name('td')
    for initial in initial_data:
        page_links = initial.find_elements_by_tag_name('a')
        for page in page_links:
            page_link = page.get_attribute("href")
            links_total.append(page_link)
    driver.refresh()
    if driver.find_element_by_partial_link_text('next'):
        next_page = driver.find_element_by_partial_link_text('next')
        next_page.click()
        time.sleep(2)
        new_data = driver.find_elements_by_tag_name('td')
        for new in new_data:
            links = new.find_elements_by_tag_name('a')
            for link in links:
                new_link = link.get_attribute("href")
                links_total.append(new_link)



for i in range(1, 23):
    first_links()


for link in links_total:
    print(link)

【问题讨论】:

    标签: python python-3.x selenium web-scraping


    【解决方案1】:

    Try-catch 会是更好的选择

    from selenium import webdriver
    import time
    
    from selenium.common.exceptions import ElementNotVisibleException, NoSuchElementException
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    options = webdriver.ChromeOptions()
    options.add_argument('headless')
    driver = webdriver.Chrome('C:/Users/../Downloads/cd79/chromedriver.exe', options=options)
    
    driver.implicitly_wait(10)
    # links_total = []
    driver.get("https://www.cbp.gov/contact/find-broker-by-port?field_port_location_tid=All&field_port_code_value=")
    
    def first_links(links_total=[]):
        initial_data = driver.find_elements_by_tag_name('td')
        for initial in initial_data:
            page_links = initial.find_elements_by_tag_name('a')
            for page in page_links:
                page_link = page.get_attribute("href")
                links_total.append(page_link)
        # driver.refresh()
        try:
          next_page = driver.find_element_by_partial_link_text('next')
          next_page.click()
          time.sleep(2)
          first_links(links_total) 
        except (TimeoutError, ElementNotVisibleException, NoSuchElementException):
          print("NEXT btn not found : ")
          pass
    
        return links_total
    
    all_links = first_links()
    
    for link in all_links:
        print(link)
    

    您实际上不需要使用 Selenium。你可以像这样用 BeautifulSoap 做到这一点:

    import requests
    from bs4 import BeautifulSoup
    
    page_num=0
    url_cbp = r"https://www.cbp.gov/contact/find-broker-by-port?field_port_location_tid=All&field_port_code_value=&page={}"
    
    def get_links(links_total=[], page_num=0):
      page = requests.get(url_cbp.format(page_num))
      soup = BeautifulSoup(page.content, 'html.parser')
      results = soup.find(id='region-content')
    
      table_cells = results.find_all('td', class_='views-field')
      for cell in table_cells:
        # print(cell )
        # print('\n\n')
        cell_link = cell.find('a')
        page_link = cell_link["href"]
        links_total.append(page_link)
    
      next_page = results.find('li', class_='pager-next')
    
      if next_page:
        page_num += 1
        get_links(links_total, page_num)
    
      return links_total
    
    all_links = get_links()
    
    for link in all_links:
      print(link)   
    

    【讨论】:

    • ..这个问题是..它只运行在下一页(即第一页和第二页)。我无法将 try catch 放在一个循环中,它会一直单击下一页,直到没有下一个元素。
    • 代码时钟 try 里面的东西是你想循环执行的吗?
    • 如你所见..这只会检查下一页。它无法返回或单击下一页。这就是问题所在。只需简单浏览一下网页即可。获得这些链接后..还有另一组不同数量的页面链接..因此,如果“下一个”选项不存在,刮板也必须返回。否则,它只会在嵌入第一个链接结束的链接之后结束。它需要回来并为系列中的下一个链接做同样的事情。
    • 我已经更新了代码。您可以尝试使用这种递归方法。
    • 我猜你没有运行它。这又不会回来了。当没有“下一个”选项时,这只会结​​束。代码错误“ raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"partial link text","selector":"next "} (Session info: headless chrome=81.0.4044.138)"..这比看起来要复杂一些。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    • 2020-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多