Home > Blockchain >  How to create multiple csv file with different name by using loop?
How to create multiple csv file with different name by using loop?

Time:08-16

Firstly I'm sorry for my poor English.. I'm kinda New with Python. So, I have a bunch of Python code to extract data from Youtube Homepage channel using Selenium Package and I wanted to store the data in CSV, the code worked so far, the only thing confuse me is, how can I naming my csv file with current working for loop?

import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome import service
from selenium.webdriver.common.keys import Keys
import time
import wget
import os
import pandas as pd
import matplotlib.pyplot as plt


urls = [
    'https://www.youtube.com/permalink_1', 
    'https://www.youtube.com/permalink_2', 
    'https://www.youtube.com/permalink_3', 
    'https://www.youtube.com/permalink_4', 
    'https://www.youtube.com/permalink_5', 
    'https://www.youtube.com/permalink_6',
    'https://www.youtube.com/permalink_7',
    'https://www.youtube.com/permalink_8',
    'https://www.youtube.com/permalink_9',
    'https://www.youtube.com/permalink_10',
    'https://www.youtube.com/permalink_11',
    'https://www.youtube.com/permalink_12',
    'https://www.youtube.com/permalink_13',
    'https://www.youtube.com/permalink_14'
]

channel_name = [
    "X_1",
    "X_2",
    "X_3",
    "X_4",
    "X_5",
    "X_6",
    "X_7",
    "X_8",
    "X_9",
    "X_10",
    "X_11",
    "X_12",
    "X_13",
    "X_14"
]


for url in urls:
    PATH = 'C:\webdrivers\chromedriver.exe.'
    driver = webdriver.Chrome(PATH)
    driver.get(url)
    #driver.maximize_window()
    driver.implicitly_wait(10)

    for i in range(30):
        driver.find_element(By.TAG_NAME, "Body").send_keys(Keys.END)
        driver.implicitly_wait(20)

    time.sleep(5)

    vid_title = []
    viewers = []
    posted_date = []

    titles = driver.find_elements(By.XPATH, "//a[@id='video-title']")
    views = driver.find_elements(By.XPATH, "//div[@id='metadata-line']/span[1]")
    when = driver.find_elements(By.XPATH, "//div[@id='metadata-line']/span[2]")

    for title in titles:
        vid_title.append(title.text)
        driver.implicitly_wait(5)
    for view in views:
        viewers.append(view.text)
        driver.implicitly_wait(5)
    for posted in when:
        posted_date.append(posted.text)
        driver.implicitly_wait(5)

    vid_item = {
        "video_title" : vid_title,
        "views" : viewers,
        "date_posted" : posted_date
    }

    df = pd.DataFrame(vid_item, columns=["video_title", "views", "date_posted"])
    #df_new = df.transpose()
    print(df)
         

The code above works as it should, how can I naming my csv file using list in channel_name (list in channel name and urls are in the same order), so when first url working, the csv also naming my csv with first list in channel_name (my expectation). Is it possible to do with this way inside the loop??

df.to_csv(r"C:\Users\........\IG_{channel_name}.csv", sep=",")

Thank you so much in advance

CodePudding user response:

To save the csv for every url in a different file you can use formatted string inside the loop

filename = url.split('_')[1]
df.to_csv(rf"C:\Users\........\IG_{filename}.csv", sep=",")
  • Related