Web scraper无法单击分页按钮

2024-04-20 13:09:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用selenium和geckodriver(在Firefox上)来刮eBay。我的操作系统是ubuntu16.04。你知道吗

我只想点击下一个按钮!我做错什么了?我已经评论了两个不起作用的按钮分配实例。。。你知道吗

# import libraries
import urllib.request
from bs4 import BeautifulSoup
from selenium import webdriver
import time
import pandas as pd 
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

# specify the url

urlpage = 'https://www.ebay.com/b/Nike-Athletic-Apparel-for-Women/185082/bn_648725?rt=nc&LH_Sold=1' 
print(urlpage)

# run firefox webdriver from executable path of your choice 
driver = webdriver.Firefox()

# get web page
driver.get(urlpage)


for page_num in range(0, 2):
    parentElement = driver.find_element_by_class_name("s-item")
    results = parentElement.find_elements_by_css_selector("*") # all children by CSS
    #button = driver.find_elements_by_class_name('ebayui-pagination__control') # not working
    #button = driver.find_elements_by_xpath('//html/body/div[3]/div[3]/div[4]/section[1]/div[2]/nav/a[2]/span/svg[2]/use') # not working
    button.click()

    print('Number of results', len(results))
    for r in results:
        print(r.text)

df = pd.DataFrame(results)
df.head()
df.to_csv('eBay_scrape.csv')

driver.quit()

收到错误:

https://www.ebay.com/b/Nike-Athletic-Apparel-for-Women/185082/bn_648725?rt=nc&LH_Sold=1

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-2-58b4e0e554fc> in <module>
     19     #results = parentElement.find_elements_by_tag_name("li") # not working...
     20     #results = driver.find_elements_by_class_name("vip") # 50 results per page. But useless...
---> 21     button = driver.find_elements_by_class_name('ebayui-pagination__control')
     22     #button = driver.find_elements_by_xpath('//html/body/div[3]/div[3]/div[4]/section[1]/div[2]/nav/a[2]/span/svg[2]/use')

IndexError: list index out of range


Tags: namefromimportdivforbydriverselenium
3条回答

driver.find_elements_by_class_name('ebayui-pagination__control')返回一个列表

该页面上有两个按钮与该类相关-要检查,请在Firefox控制台中键入:$$('.ebayui-pagination__control')

所以你需要: button = driver.find_elements_by_class_name('ebayui-pagination__control')[1]获取第二个按钮。你知道吗

第二种方法(通过xpath查找元素)看起来非常脆弱,因为xpath很长,只需要一个数组在该路径中发生变化,即使您一开始就让它工作,它也不再工作。你知道吗

你可以诱导WebDriverWaitelement_to_be_clickablexpath。你知道吗

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

WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//a[@class='ebayui-pagination__control'][@rel='next']"))).click()

你不需要通过代码点击下一页按钮,只需要更新你的抓取网址。你知道吗

如果您注意到,&_pgn=<page_number>会附加到后续页面的url字符串中。您可以简单地刮取一页并增加页码,直到没有有效的页码为止。你知道吗

相关问题 更多 >