【问题标题】:pandas: how write data to csv file in pythonpandas:如何在 python 中将数据写入 csv 文件
【发布时间】:2021-08-12 07:06:04
【问题描述】:

作为一个新手,我不知道如何编写 csv 文件帮助将数据存储在 csv 文件中。 我希望文件头应该是这样的,并且在标题下方,数据应该是外来的。提前致谢 (0,0,"Product_Url") (0,1,"Product_Manufacturer") (0,2,"Product_Model") (0,3,"Product_color") (0,4,"Product_memory") (0,5,"Product_Price") (0,6,"货币") (0,7,"增值税") (0,8,"运费") (0,9,"国家") (0,10,"日期")

import xlwt
from selenium import webdriver
import re
import time
from datetime import date

class expert:
    def __init__(self):
        self.url='https://expert938.expertonline.it/dm-IT-it/Vendita_Smartphone_W8D.aspx'
        self.country='IT'
        self.currency='euro'
        self.VAT='Included'
    def experts(self):
        try:
            driver=webdriver.Chrome()            
            driver.get(self.url)
            today = date.today()
            time.sleep(5)
            driver.maximize_window()
            x=1           
            while True:             
                containers = []
                flag = False
                containers =driver.find_elements_by_css_selector('div[class="col-xs-12 skywalker_riga skywalker_riga_articolo"]')                 
                for container in containers:                      
                    try:
                        url = container.find_element_by_css_selector('div[class="skywalker_riga_nome"]')
                        urls = url.find_element_by_tag_name('a').get_attribute('href')
                        if urls != "":
                            print(urls)
                            flag = True
                    except:
                        pass
                    try:
                        title = container.find_element_by_css_selector('div[class="skywalker_riga_nome"] h3').text
                        if title != "":
                            print(title)
                            flag = True
                    except:
                        pass
                    try:
                        price = container.find_element_by_css_selector('div[class="skywalker_riga_prezzo"]').text
                        if price  != "":
                            print(price)
                            flag = True
                    except:
                        pass
                print("flag value is:" +str(flag))   
                if flag==False:
                    break
                try:
                    x+=1
                    time.sleep(5)
                    driver.get(self.url+"?pagina="+str(x))                    
                    print("next page") 
                except:
                    break
        except:
            pass
expertit=expert()
expertit.experts() 

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    Pandas dataframe to CSV 可以轻松做到这一点,

    第 1 步: Start with an empty dataframe 并在循环时填充它

    pypd_df1 = pd.DataFrame(data, columns=["Product_Url","Product_Manufacturer","Product_Model","Product_color","Product_memory","Product_Price","Currency","VAT","Shipping Cost","Country","Date"])
    

    第 2 步: Add rows to existing dataframeconsider this while you deal with python 中的数据帧。

    pypd_df1.loc[len(pypd_df1)] = [Product_Url_var_name, Product_Manufacturer_var_name, ...]
    
    

    您有时会发现Python's writelines 方法很有趣。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-21
      • 1970-01-01
      • 2016-12-30
      • 2015-01-21
      • 2016-02-10
      • 1970-01-01
      • 1970-01-01
      • 2017-03-29
      相关资源
      最近更新 更多