【发布时间】:2020-11-19 04:17:12
【问题描述】:
我有一个基本的 bs4 网络爬虫,获取我的爬取数据没有问题,但是当我尝试将其写入 .csv 文件时,我遇到了一些问题。我无法将数据写入多个列。在the tutorial 我有点关注,他可以很容易地用“,”分隔行,但是当我用 excel 打开我的 CSV 时,无论是在标题中还是在数据中都没有分隔符,我错过了什么?
import requests
from bs4 import BeautifulSoup
url="myurl"
page=requests.get(url)
soup=BeautifulSoup(page.content,'html.parser')
items=soup.find_all('a', class_='listing-card')
filename = 'data.csv'
f = open(filename, "w")
header = "name, price\n"
f.write(header)
for item in items:
title = item.find('span', class_='title').text
price = item.find('span', class_='price').text
f.write(title.replace(",","|") + ',' + price + "\n")
f.close()
【问题讨论】:
-
当您在文本编辑器中打开文件时,
data.csv的外观如何?
标签: python dataframe csv web-scraping beautifulsoup