Python将csv文件格式化为一个lin

2024-04-26 09:45:21 发布

您现在位置:Python中文网/ 问答频道 /正文

在从网站抓取第一组文本,然后是价格之后,很难用逗号将简历格式化为一行。我想做的是把产品信息和价格放在一行,在产品信息后面用逗号隔开,这样就可以导入Excel电子表格。有什么线索吗?谢谢

import requests
from bs4 import BeautifulSoup
import csv

b6 = open('sears.csv', 'w', newline='') 
a6 = csv.writer(b6,delimiter=',')

soup = BeautifulSoup(requests.get("http://www.sears.ca/catalog/appliances-fridges-freezers-refrigerators-top-freezer-en-wp-836#facet:&productBeginIndex:0&orderBy:&pageView:grid&minPrice:&maxPrice:&pageSize:100&").text)

g6_data = soup.select("div.product_name a")
p6_data = soup.select("div.product_price")

for g6, p6 in zip(g6_data, p6_data):
    c6 = (g6.text, p6.text)
    print(g6.text, p6.text)
    a6.writerow(c6)

b6.close()

Tags: csvtextimport信息data产品价格requests
1条回答
网友
1楼 · 发布于 2024-04-26 09:45:21

您的请求是可以的,但用BeautifulSoup进行的筛选是不可以的。你知道吗

import requests
from BeautifulSoup import BeautifulSoup

soup = BeautifulSoup(requests.get("http://www.sears.ca/catalog/appliances-fridges-freezers-refrigerators-top-freezer-en-wp-836#facet:&productBeginIndex:0&orderBy:&pageView:grid&minPrice:&maxPrice:&pageSize:100&").text)

g_descrs = [i.find("a").text for i in soup.findAll("div", {"class": "product_name", "itemprop": True})]
g_prices = [i.find("input").get('value') for i in soup.findAll("div", {"class": "product_price"})]

rows = map(lambda x: '%s;%s' % x, zip(g_descrs, g_prices))

with open('./result.csv', 'w') as result:
    for row in rows:
        result.write(row)

相关问题 更多 >