如何将产品信息导出到csv并创建6列?(python, csv)

How to export product information to csv and create 6 columns?(python, csv)

我使用 selenium 创建了一个 python 程序,我想将信息导出到 csv 并创建 6 列(产品名称、产品价格、产品重量、 产品维度、产品 ASIN、购物车中的商品)。前 5 个产品信息来自第一个亚马逊页面,第 6 个信息来自购物车页面。现在我打印前 5 列并将第 6 列导出到 csv,但我想创建一个包含所有这 6 列和这些产品信息的 csv。我附上了代码。我将不胜感激任何帮助。

from selenium import webdriver
from time import sleep
from selenium.webdriver.common.keys import Keys
import csv
proxies = {
    'http': 'http://31.173.209.111:8080',
    'https': 'http://118.179.60.110:8080',
}

url = "https://www.amazon.com/Haggar-Hidden-Comfort-Waist-Plain/dp/B0018Q3BRO/ref=sr_1_3?ie=UTF8&qid=1518006207&sr=8-3&keywords=trousers+for+men"

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % proxies)

driver = webdriver.Chrome(executable_path="C:\Users\Andrei\Downloads\chromedriver_win32\chromedriver.exe",
                          chrome_options=chrome_options)
driver.get(url)

product_title = driver.find_elements_by_xpath('//*[@id="productTitle"][1]')
product_price = driver.find_elements_by_xpath('//*[@id="priceblock_ourprice"][1]')
product_weight = driver.find_elements_by_xpath('//*[@id="detailBullets_feature_div"]/ul/li[2]/span/span')
product_dimension = driver.find_elements_by_xpath('//*[@id="detailBullets_feature_div"]/ul/li[1]/span/span')
product_asin = driver.find_elements_by_xpath('//*[@id="detailBullets_feature_div"]/ul/li[3]/span/span')
product_reviews = driver.find_elements_by_xpath('//*[@id="detailBullets_averageCustomerReviews"]/span/span')
prod_title = [x.text for x in product_title]
prod_price = [x.text for x in product_price]
prod_weight = [x.text for x in product_weight]
prod_dimension = [x.text for x in product_dimension]
prod_asin = [x.text for x in product_asin]
prod_reviews = [x.text for x in product_reviews]
print(prod_title[0])
print(prod_price[0])
print(prod_weight[1])
print(prod_dimension[1])
print(prod_asin[1])

try:
    sizemenu = driver.find_element_by_id('dropdown_selected_size_name')
    sizemenu.click()
    select = driver.find_element_by_id('size_name_1')  # medium size
    select.click()
except:
    print('no select')

sleep(3)

driver.find_element_by_xpath('//*[@id="submit.add-to-cart"]/span/input').click()

# driver.find_element_by_xpath('//*[@id="smartShelfAddToCartContinue"]/span/input')
sleep(3)
try:
    driver.execute_script("document.getElementById('smartShelfAddToCartNative').click()")
except:
    print('no continue button')

try:
    driver.find_element_by_xpath('//*[@id="hlb-view-cart"]/span/a').click()
except:
    print('no cart button')

sleep(3)

# items_cart = driver.find_element_by_xpath('//div[@class="a-alert-content"]/span')

driver.find_element_by_xpath('//*[@id="a-autoid-0-announce"]/span[2]').click()
driver.find_element_by_xpath('//*[@id="dropdown1_9"]').click()
quantity_xpath = '//*[@id="activeCartViewForm"]/div[2]/div/div[4]/div/div[3]/div/div/input'
quantity_el = driver.find_element_by_xpath(quantity_xpath)
quantity_el.send_keys("999" + Keys.ENTER)

sleep(2)

items_cart = driver.find_elements_by_xpath('//div[@class="a-alert-content"]/span')
items_in_cart = [x.text for x in items_cart]

csvfile='products_final.csv'
# Assuming res is a flat list
with open(csvfile, "w") as output:
    writer = csv.writer(output, lineterminator='\n')
    writer.writerow(items_in_cart[1])

如果您只输出一行 header 行和一行特征,那么将它们分别创建为列表可能是最简单的方法。例如:

header = ['Product Title', 'Product Price', 'Product weight', 'Product dimension', 'Product ASIN', 'Items in cart']

并且,按照您描述的索引,

data = [prod_title[0], prod_price[0], prod_weight[1], prod_dimension[1], prod_asin[1], items_in_cart[1]]

你可以这样写这些结果:

with open(csvfile, "w") as output:
    writer = csv.writer(output)
    writer.writerow(header)
    writer.writerow(data)