Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python(Selenium)-如何将从第1页到最后一页的数据保存到同一个CSV_Python_Html_Selenium - Fatal编程技术网

Python(Selenium)-如何将从第1页到最后一页的数据保存到同一个CSV

Python(Selenium)-如何将从第1页到最后一页的数据保存到同一个CSV,python,html,selenium,Python,Html,Selenium,我有一个网页,我需要使用Python和selenium从嵌入到第1页最后一页的表中刮取所有数据 以下是网站: 我需要帮助使代码转到下一页,直到到达最后一页。挑战在于,与“下一个”或“上一个”这样的导航按钮不同,此网页将编号索引作为按钮进行导航。因此,很难在逻辑中应用HTML元素 另外,如何将每个页面中的数据保存到同一个.CSV文件中?为什么列标题不保存在我的csv中 以下是我目前的测试代码: 页面导航测试: import selenium from selenium import webdriv

我有一个网页,我需要使用Python和selenium从嵌入到第1页最后一页的表中刮取所有数据

以下是网站:

我需要帮助使代码转到下一页,直到到达最后一页。挑战在于,与“下一个”或“上一个”这样的导航按钮不同,此网页将编号索引作为按钮进行导航。因此,很难在逻辑中应用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”选项声明功能,该选项表示“追加”,以便将数据追加到同一个文件中

你到底被困在哪里?哪一行?你看到任何错误了吗?当保存此页上的数据时,我一直在让代码转到下一页。另外,我不知道如何将每个页面的表中的数据保存到同一个CSV文件中。理想情况下,我希望将这两个代码部分组合在一起