【发布时间】:2021-01-30 18:04:38
【问题描述】:
我正在使用来自 pacsun.com 的 python 进行网络抓取,我正在尝试将其放入 csv 文件中,但是当我打开文件时,只打印标题,而不是产品名称、价格或新到货。
所以我的问题是如何让这些值在 csv 文件的标题下打印出来?
from bs4 import BeautifulSoup as soup
import csv
my_url = ('https://www.pacsun.com/mens/')
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
page_soup = soup(page_html, 'html.parser')
product_data = page_soup.findAll('div',{'class':'product-data'})
#print(len(product_data))
#print(product_data[0])
product = product_data[0]
filename = 'pacsun.csv'
f = open(filename,'w')
headers = 'product_name, price, new_arrival\n'
f.write(headers)
for product in product_data:
#name = product.div.a["title"]
product_name = print('product: ' + product.div.a["title"])
#the code above gets the title of the product
price = product.findAll('div',{'class':'product-price group'})
#the code above gets the price of the product
new_arrival = product.findAll('div',{'class':'new'})
#the code above gets
print(price[0].text)
print(new_arrival[0].text)
thewriter = csv.DictWriter(filename, headers)
thewriter.writerow({'product_name':product_name, 'price':price, 'new_arrival':new_arrival})
#f.write(product_name.replace(",", "|") + "," + price + ","+ new_arrival + "\n")
f.close()
【问题讨论】:
-
尝试将其更改为
f = open(filename,'a')并将f.write放入循环中 -
我试过了,它打印了两次标题
标签: python-3.x csv web-scraping