如何将两个单独的for循环数据放入CSV文件中

时间:2017-04-06 05:30:35

标签: python csv for-loop

如何将两个单独的for loop数据放入CSV文件中。第一列名称为link,第二列名称为mobile_name

  

期待输出:result.csv

link,mobile_name
https://www.flipkart.com/moto-g5-plus-fine-gold-32-gb/p/itmes2zjvwfncxxr?pid=MOBEQHMGED7F9CZ2&srno=b_1_1&otracker=browse
&lid=LSTMOBEQHMGED7F9CZ2KHTBI8,Moto G5 Plus (Fine Gold, 32 GB)
  

代码:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
import time

chrome_path = r"C:\Users\Venkatesh\AppData\Local\Programs\Python\Python35\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
#driver.get("https://www.flipkart.com/mobiles")
driver.get("https://www.flipkart.com/mobiles")

search = driver.find_element_by_xpath("""//*[@id="container"]/div/div[2]/div/div[2]/div/div/div[1]/section/div[3]/div/div/a""").click()
delay = 90 # seconds
try:
    #posts = WebDriverWait(driver, delay).until(EC.presence_of_element_located(driver.find_elements_by_class_name("iUmrbN")))
    #posts = WebDriverWait(driver, delay).until(EC.presence_of_element_located(By.XPATH, "//*[@id='container']/div/div[2]/div/div[2]/div/div/div[1]/a/div[2]"))
    #posts = driver.find_elements_by_class_name("iUmrbN")
    #posts = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.XPATH, "//*[@id='container']/div/div[2]/div/div[2]/div/div/div[1]/a/div[2]")))
    WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.XPATH, "//*[@id='container']/div/div[2]/div[2]/div/div[2]/div/div[3]/div[1]/div/div[1]/a/div[2]/div[1]/div[1]")))
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    print("Page is ready")
except TimeoutException:
    print("Loading took too much time")
#print(driver.find_elements_by_class_name("_1UoZlX").get_attribute("href"))
time.sleep(10)
for post in driver.find_elements_by_class_name("_1UoZlX"):
    link = post.get_attribute("href")
    print(link)
for post1 in driver.find_elements_by_class_name("_3wU53n"):
    mobile_name = post1.text
    print(mobile_name)
  

修改后的代码:working fine

link = []
mobile_name = []
courses_list=[]
for post in driver.find_elements_by_class_name("_1UoZlX"):
    link += [ post.get_attribute("href") ]
for post1 in driver.find_elements_by_class_name("_3wU53n"):
    mobile_name += [ post1.text ] 

link_mobile_name = []
for i in range(len(link)) : 
    link_mobile_name = [ link[i], mobile_name[i] ]
    print(courses_list.append(link_mobile_name))
time.sleep(2)
driver.quit()

with open ('result.csv','w', newline='') as file:
    writer=csv.writer(file)
    writer.writerow(['product_link'])
    for row in courses_list:
        writer.writerow(row)

1 个答案:

答案 0 :(得分:0)

以下是代码:

link = []
mobile_name = []
for post in driver.find_elements_by_class_name("_1UoZlX"):
    link += [ post.get_attribute("href") ]
for post1 in driver.find_elements_by_class_name("_3wU53n"):
    mobile_name += [ post1.text ] 

link_mobile_name = []
for i in range(len(link)) : 
    link_mobile_name += [ [ link[i], mobile_name[i] ] ]

注意:我假设linkmobile_name的长度相同。