Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将产品信息导出到csv并创建6列?(python,csv)_Python_Csv_Selenium_Amazon_Scrape - Fatal编程技术网

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

如何将产品信息导出到csv并创建6列?(python,csv),python,csv,selenium,amazon,scrape,Python,Csv,Selenium,Amazon,Scrape,我使用selenium创建了一个python程序,我想将信息导出到csv并创建6列(产品标题、产品价格、产品重量、, 产品维度、产品ASIN、购物车中的项目)。前5个产品信息来自第一个亚马逊页面,第6个信息来自购物车页面。现在我打印前5列,并将第6列导出为csv,但我想创建一个包含所有这6列产品信息的csv。我附上了密码。我将感谢任何帮助 从selenium导入webdriver 从时间上导入睡眠 从selenium.webdriver.common.keys导入密钥 导入csv 代理={ '

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

从selenium导入webdriver
从时间上导入睡眠
从selenium.webdriver.common.keys导入密钥
导入csv
代理={
'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=裤子+男款”
chrome\u options=webdriver.ChromeOptions()
chrome\u选项。添加\u参数('--proxy server=%s'%proxy)
driver=webdriver.Chrome(可执行文件\u path=“C:\\Users\Andrei\Downloads\chromedriver\u win32\chromedriver.exe”,
chrome\u选项=chrome\u选项)
获取驱动程序(url)
product_title=driver。通过xpath('/*[@id=“productTitle”][1]'查找元素
product\u price=driver。通过xpath('/*[@id=“priceblock\u ourprice”][1]'查找\u元素
product\u weight=driver。通过xpath('/*[@id=“detailbollets\u feature\u div”]/ul/li[2]/span/span'查找元素
产品维度=驱动程序。通过xpath('/*[@id=“detailbollets\u feature\u div”]/ul/li[1]/span/span'查找元素
product\u asin=driver。通过xpath查找元素('/*[@id=“detailbollets\u feature\u div”]/ul/li[3]/span/span')
product_reviews=driver。通过xpath('/*[@id=“detailbollets\u averageCustomerReviews”]/span/span'查找元素
产品标题=[x.text代表产品标题中的x]
产品价格=[x.产品价格中x的文本]
产品重量=[x.产品重量中x的文本]
产品维度=[x.text代表产品维度中的x]
prod_asin=[x.text代表产品中的x]
prod_reviews=[x.text代表产品_reviews中的x]
打印(产品标题[0])
打印(产品价格[0])
打印(产品重量[1])
打印(产品尺寸[1])
打印(产品asin[1])
尝试:
sizemenu=驱动程序。按\u id查找\u元素('下拉菜单\u选定\u大小\u名称')
sizemenu.click()
select=driver。按_id(“大小_名称_1”)35;中等大小查找_元素
选择。单击()
除:
打印('无选择')
睡眠(3)
驱动程序。通过xpath('/*[@id=“submit.add to cart”]/span/input')查找元素。单击()
#驱动程序。通过xpath('/*[@id=“smartShelfAddToCartContinue”]/span/input'查找元素
睡眠(3)
尝试:
driver.execute_脚本(“document.getElementById('smartsheldtocartnative')。click()”)
除:
打印('无继续按钮')
尝试:
驱动程序。通过xpath('/*[@id=“hlb view cart”]/span/a')查找元素。单击()
除:
打印('无购物车按钮')
睡眠(3)
#items\u cart=driver。通过xpath('//div[@class=“a-alert-content”]/span'查找元素
驱动程序。通过xpath('/*[@id=“a-autoid-0-announce”]/span[2]”查找元素。单击()
驱动程序。通过xpath('/*[@id=“dropdown1_9”]”查找元素。单击()
quantity_xpath='/*[@id=“activeCartViewForm”]/div[2]/div/div[4]/div/div/div[3]/div/div/input'
quantity\u el=驱动程序。通过xpath(quantity\u xpath)查找元素
数量发送键(“999”+键。输入)
睡眠(2)
items\u cart=driver。通过xpath('//div[@class=“a-alert-content”]/span'查找元素
购物车中的商品=[x.text代表购物车中的商品]
csvfile='products\u final.csv'
#假设res是一个平面列表
以open(csvfile,“w”)作为输出:
writer=csv.writer(输出,行终止符='\n')

writer.writerow(购物车[1]中的项目)
如果您只输出一行标题行和一行功能,可能最简单的方法是将它们创建为列表。例如:

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)

这些列表的长度相同吗?我编辑了代码。但是每个列表中有多少项?就一个?也就是说,您的.csv将有6列,其中有一行标题和一行功能?我设置了索引,例如prod_title[0],因此它将打印一个项目。在产品中标题是1项,在产品中价格是1项,在产品中重量是2项,产品维度是2项,产品是2项,在购物车中是2项,但我将索引设置为0或1,所以它为每个列表返回一项。是的,我的csv将有6列,一个标题和一行。产品页面中有5个产品信息,购物车页面中有一个产品信息。它返回writer.writerow(header)TypeError:需要一个类似字节的对象,而不是'str'完美。非常感谢。