【发布时间】:2021-08-19 19:33:40
【问题描述】:
这是我抓取的第一个网页,我发现的其他一些解决方案似乎没有太大帮助。正如您将看到的,“下一步”按钮仍然可见,但是当您到达最后一页时,CSS 会发生一些变化。
一些笔记。我正在使用 python、selenium 和 google chrome。
我正在尝试遍历此页面上表格的每个部分:https://caearlyvoting.sos.ca.gov/
我已经弄清楚如何遍历每个县,并获取我需要的信息(我认为)。但是,当表格的记录多于默认显示的 10 条记录时,我对如何移动到下一页感到困惑。
我已经尝试过这种变体
try:
next_page = driver.find_element_by_class_name('paginate_button')
next_page.click()
except NoSuchElementException:
pass
但没有运气。我尝试以不同的方式获取元素,但遇到了同样的问题。
谁能帮我弄清楚如何点击每个页面,抓住我需要的东西,然后移动到下一个县?我不需要帮助从表格中获取信息,只需单击页面然后移动到下一个县。
编辑 这是基于后续的代码的其余部分。我在构建它时遇到了困难。
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import pandas as pd
import time # not for production
# Name of the counties Single column with county names
county_df = pd.read_csv('Counties.csv')
# Path to driver on this computer
chrome_driver_path = r'C:\Windows\chromedriver'
# url to scrape
url = 'https://caearlyvoting.sos.ca.gov/'
with webdriver.Chrome(executable_path=chrome_driver_path)as driver:
# Open window, maximize and set an implicit wait
driver.get(url)
driver.maximize_window()
driver.implicitly_wait(10)
actions = ActionChains(driver) #* New line here from stackoverflow
# find the county selection
county_selector = driver.find_element_by_id('CountyID')
# for loop tomove through the counties
for county in county_df['County'][:5]:
# Input the county namne
county_selector.send_keys(county)
### Code to grab data goes here
########* Code from stackoverflow ########
while True:
next_page = driver.find_element_by_css_selector(".paginate_button.next")
next_bnt_classes = next_page.get_attribute("class")
if "disabled" in next_bnt_classes:
break #last page reached, no more next pages, break the loop
else:
actions.move_to_element(next_page).perform()
time.sleep(0.5)
#get the actual next page button and click it
driver.find_element_by_css_selector(".paginate_button.next a").click()
【问题讨论】:
标签: python web-scraping selenium-chromedriver