简体   繁体   English

如何将 python 中的数据逐行写入 csv 文件?

[英]How do I write data from python line by line to csv file?

I'm just learning python.我只是在学习 python。 I want to improve myself with examples.我想通过例子来提高自己。 sorry for my English.对不起我的英语不好。 I'm in the process of learning a new language.我正在学习一门新语言。 :) :)

The program pulls data from an e-commerce site.该程序从电子商务网站提取数据。

when I want to save it as a csv file, each new data overwrites the previous data.当我想将其保存为 csv 文件时,每个新数据都会覆盖以前的数据。 I tried several examples but it didn't work.我尝试了几个例子,但没有奏效。

Thanks for your help.谢谢你的帮助。

import requests 
import gettext
from bs4 import BeautifulSoup
import pandas as pd
import openpyxl as xls 
import xlsxwriter`

baseurl = "https://www.trendyol.com"

headers = {'User-Agent': 
           'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36 OPR/38.0.2220.41'
          }

for x in range(1,62):
    r = requests.get(f'https://www.trendyol.com/cep-telefonu-x-c103498?pi={x}', headers=headers)
    soup = BeautifulSoup(r.content, 'lxml')
    
    productlist = soup.find_all('div', class_='p-card-wrppr')

    for item in productlist:
        
        productname = item.find('span', class_='prdct-desc-cntnr-name').getText()
        productprice_old = item.find('div', class_='prc-box-sllng').getText()
        productprice_discount = item.find('div', class_='prc-box-dscntd') 
        
        for productlink in item.find_all('a'):
            productlink = baseurl+productlink.get('href') 

            if productprice_discount == None:
                productprice_discount = productprice_old
            else: 
                productprice_discount = productprice_discount.getText()          
                
            for merchant_name in productlink:
                r = requests.get(productlink, headers=headers)
                soup = BeautifulSoup(r.content, 'lxml')   

                merchant_name = soup.find('a', class_='merchant-text')

                if merchant_name == None: 
                    merchant_name = soup.find('a', class_='title')
                    
                    if merchant_name == None:
                        merchant_name = soup.find('span', class_='product-description-market-place')
                    
                    if merchant_name == None:
                            merchant_name = ('NULL')
                else: 
                    merchant_name = merchant_name.getText()
                
                break

            for product_image in productlink:
                r = requests.get(productlink, headers=headers)
                soup = BeautifulSoup(r.content, 'lxml')
                product_image = soup.find_all('img', attrs={'class':'detail-section-img'})   
                
            
                image_src = [x['src'] for x in product_image]
                image_src = [x for x in image_src if x.endswith('.jpg' or '.png')]
                
            break


        data = [ [productname,productlink,productprice_old,productprice_discount,merchant_name,image_src] ]
        df = pd.DataFrame(data, columns = ["Product Name", "URL", "Price", "D-Price", "Store", "Image Url"])
        df.to_csv('trendyol3.csv')

You should add mode='a' , which means append to append to file instead of rewriting:您应该添加mode='a' ,这意味着append到 append 文件而不是重写:

df.to_csv('trendyol3.csv', mode='a')

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

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