简体   繁体   English

使用 Python 在 csv 中附加数据

[英]Appending data in csv using Python

Why is the data on my existing csv will not stack up but instead it will rewrite the data of the previous page.为什么我现有的csv上的数据不会叠加,而是会重写上一页的数据。

for x in range(1,5):
    r = 'https://www.nda-toys.com/29/games-wholesale?page='
    url = r+ str(x)
    driver.get(url)
    
    elements = driver.find_elements_by_class_name('catConttop')
    links = [] 
    datalist =[]

    for ele in elements:
        links += (ele.find_element_by_tag_name('a').get_attribute('href'),) 
      
        
        for link in links:
            driver.get(link)
        
            name = driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[1]/span[2]/h3/strong').text
            code = driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[1]/span[2]/table/tbody/tr[2]/td[2]').text
            price = driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[1]/span[3]/span[1]').text
            avail = driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[1]/span[3]/span[3]/strong').text
        
            df_text =[name,code,price,avail,link] 
            datalist.append((name,code,price,avail,link))
            ColName=['carName','Loc','price','avail','link']
        
        df = pd.DataFrame(datalist,columns=['name','code','price','avail','link'])
        df.to_csv(r'C:\Users\Ryzen 5\Desktop\work\hannah.csv',index=True,encoding='utf-8') 

You will need to create your main dataframe outside of your first for loop and append to that at the end of each loop.您需要在第一个 for 循环之外创建主 dataframe 并在每个循环结束时创建 append。 Finally you will export that main df to csv at the end outside the loop.最后,您将在循环外的末尾将该主 df 导出到 csv。 Otherwise you are rewriting it and exporting it with each page loop.否则,您将重写它并在每个页面循环中导出它。

# make main df
# for loop on each page:
    # logic to format each page
    # append to main df
# export csv

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM