【问题标题】:How to write to a csv from from python如何从python写入csv
【发布时间】: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


【解决方案1】:

您的数据有问题。所以,我修复了它,它工作正常。您只需将w 更改为af = open(filename,'a') 并将f.write 放入循环中

from bs4 import BeautifulSoup as soup
import csv
from urllib.request import urlopen as uReq
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,"a")
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'})
    price_ = ''
    new_arrival_ = ''
    product_name_ = ''
    # product_name_ = ' '.join([str(elem) for elem in  product.div.a["title"]])
    for price_text in price:
        price_ = price_text.text
    for new_arrival_text in new_arrival:
        new_arrival_ = new_arrival_text.text


    f.write(product.div.a["title"]+","+price_+ "," + new_arrival_ + "\n")

f.close()

【讨论】:

    猜你喜欢
    • 2014-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多