0

As a fresher I don't know how to write a csv file help to store a data in csv file. I wanted file header should be like this and below the header, data should be aliened. thanks in advance (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,"Currency") (0,7,"VAT") (0,8,"Shipping Cost") (0,9,"Country") (0,10,"Date")

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

1 Answer 1

1

Pandas dataframe to CSV can do this easily,

Step1: Start with an empty dataframe and fill it while you loop

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

Step2: Add rows to existing dataframe or consider this while you deal with dataframes in python.

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

You may find Python's writelines method interesting sometimes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.