![](/img/trans.png)
[英]How to go on last page from number of pages using selenium python?
[英]Python (Selenium) - How to save data from page 1 to the last page to the same CSV
我有一個網頁,我需要使用 Python 和 selenium 來抓取從第 1 頁嵌入到最后一頁的表格中的所有數據。
這是網站: https : //www.ageofempires.com/mods
我需要幫助讓代碼轉到下一頁,直到它到達最后一頁。 挑戰在於,該網頁沒有像“下一個”或“上一個”這樣的導航按鈕,而是將編號索引作為導航按鈕。 所以很難在邏輯中應用 HTML 元素。
另外,如何將每個頁面的數據保存到同一個 .CSV 文件中? 為什么列標題沒有保存在我的 csv 中?
以下是我到目前為止的測試代碼:
頁面導航測試:
import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
from selenium.common.exceptions import TimeoutException
import time
import csv
from datetime import datetime
from selenium.common.exceptions import WebDriverException
# Use driver to locate information
driver = webdriver.Edge(executable_path = "C://Windows//SysWOW64//MicrosoftWebDriver.exe")
driver.maximize_window()
# Using Edge to open the website
driver.get("https://www.ageofempires.com/mods")
driver.implicitly_wait(10)
while True:
try:
driver.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[@class='pagination']/a"))))
driver.find_element_by_xpath("//li[@class='pagination']/a").click()
print("Navigating to Next Page")
except (TimeoutException, WebDriverException) as e:
print("Last page reached")
break
driver.quit()
打開 CSV 並保存數據:
table = driver.find_element_by_css_selector('#mods-listing > table')
filename = datetime.now().strftime('C:/Users/username/Desktop/Output/ModsAll_%Y%m%d_%H%M.csv')
with open(filename, 'w', newline='') as csvfile:
wr = csv.writer(csvfile)
for row in table.find_elements_by_css_selector('tr'):
wr.writerow([d.text for d in row.find_elements_by_css_selector('td')])
我自己想出來的:
table = driver.find_element_by_css_selector('#mods-listing > table')
filename = datetime.now().strftime('C:/Users/username/Desktop/Output/ModsAll_%Y%m%d_%H%M.csv')
with open(filename, 'w', newline='', encoding="utf-8") as csvfile:
wr = csv.writer(csvfile)
for row in table.find_elements_by_css_selector('tr'):
wr.writerow([d.text for d in row.find_elements_by_css_selector('td')])
driver.find_element_by_xpath('//*[@id="mods-paginav"]/ul/li[2]/button').click()
time.sleep(3)
table = driver.find_element_by_css_selector('#mods-listing > table')
with open(filename, 'a', newline='', encoding="utf-8") as csvfile:
wr = csv.writer(csvfile)
for row in table.find_elements_by_css_selector('tr'):
wr.writerow([d.text for d in row.find_elements_by_css_selector('td')])
driver.find_element_by_xpath('//*[@id="mods-paginav"]/ul/li[3]/button').click()
time.sleep(3)
table = driver.find_element_by_css_selector('#mods-listing > table')
with open(filename, 'a', newline='', encoding="utf-8") as csvfile:
wr = csv.writer(csvfile)
for row in table.find_elements_by_css_selector('tr'):
wr.writerow([d.text for d in row.find_elements_by_css_selector('td')])
i = 0
while i < 89:
driver.find_element_by_xpath('//*[@id="mods-paginav"]/ul/li[5]/button').click()
time.sleep(3)
table = driver.find_element_by_css_selector('#mods-listing > table')
with open(filename, 'a', newline='', encoding="utf-8") as csvfile:
wr = csv.writer(csvfile)
for row in table.find_elements_by_css_selector('tr'):
wr.writerow([d.text for d in row.find_elements_by_css_selector('td')])
i += 1
else:
print("This is the last page! ")
print("Finished... ")
driver.quit();
我的邏輯有點復雜,所以如果有更好的解決方案,我將不勝感激。
從網頁 4 開始,頁面按鈕/圖標的布局是穩定的,因此我可以使用 for 循環。 對於第 1、2 和 3 頁,由於按鈕布局不同,我必須單獨抓取數據。
為了繼續將數據保存到同一個 CSV 文件,只需使用“a”選項聲明該功能,這意味着“附加”,因此數據將附加到同一文件中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.