我正在尝试使用BeautifulSoup抓取一个网站

时间:2019-12-22 07:30:34

标签: html python-3.x beautifulsoup jupyter-notebook

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

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文件:

enter image description here

0 个答案:

没有答案