首页 > 解决方案 > 我试图使用 BeautifulSoup 抓取一个网站

问题描述

我已经让它在笔记本中正常打印,但我不明白为什么它在 excel 文件中显示不必要的文本。这是代码片段。

import csv


file = open('targeted_prods.csv', 'w')
writer = csv.writer(file)

writer.writerow(['Product Name', 'Brand', 'Price'])
for prod_info in prod_prev:
    prod_name = prod_info.find('div', {'class': 'product-name'}).h3.a.attrs['title'].text
    brand_name = prod_info.find('div', {'class': 'product-name'}).find('h4').text
    price = prod_info.find('div', {'class': 'product-price'}).find('span').text
    print(prod_name + ' ' + brand_name + ' ' + price)
writer.writerow([prod_name.encode('utf-8'), brand_name.encode('utf-8'), price.encode('utf-8')])

Excel文件:

在此处输入图像描述

标签: htmlpython-3.xbeautifulsoupjupyter-notebook

解决方案


推荐阅读