簡體   English   中英

如何將 python 中的數據逐行寫入 csv 文件?

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

我只是在學習 python。 我想通過例子來提高自己。 對不起我的英語不好。 我正在學習一門新語言。 :)

該程序從電子商務網站提取數據。

當我想將其保存為 csv 文件時,每個新數據都會覆蓋以前的數據。 我嘗試了幾個例子,但沒有奏效。

謝謝你的幫助。

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')

您應該添加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