html - 我试图使用 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文件:
解决方案
推荐阅读
- scala - 从别名类型的类中隐式获取 Schema
- microsoft-teams - MS Teams,深色模式,带有自适应卡的任务 - 颜色错误(Microsoft Teams 版本 1.4.00.26376(64 位)。最后更新于 2021 年 10 月 5 日。)
- ajax - 如何使从 AJAX 生成的表单工作?
- flutter - MaterialApp 作为 MediaQuery 小部件的祖先。但是怎么做?
- javascript - 使用 React Native Expo 进行 Firebase 电话身份验证
- python-3.x - 我最近安装了 ubuntu,我是 Linux 的大佬……我无法在 ubuntu 20.4 中执行命令。请帮助我
- php - Laravel 护照在 PassportUserProvider 上返回错误 null 给定
- ios - iOS 应用程序上的通用链接打开应用程序但不调用处理程序
- python - 从实时 YouTube 视频中检测对象并生成链接
- laravel - 获取最后插入或更新存储操作