【发布时间】:2020-07-15 12:54:47
【问题描述】:
我目前正在使用下面的代码来网络抓取数据,然后将其存储在 CSV 文件中。
from bs4 import BeautifulSoup
import requests
url='https://www.business-standard.com/rss/companies-101.rss'
soup = BeautifulSoup(requests.get(url).content, 'xml')
news_items = []
for item in soup.findAll('item'):
news_item = {}
news_item['title'] = item.title.text
news_item['excerpt'] = item.description.text
print(item.link.text)
s = BeautifulSoup(requests.get(item.link.text).content, 'html.parser')
news_item['text'] = s.select_one('.p-content').get_text(strip=True, separator=' ')
news_item['link'] = item.link.text
news_item['pubDate'] = item.pubDate.text
news_item['Category'] = 'Company'
news_items.append(news_item)
import pandas as pd
df = pd.DataFrame(news_items)
df.to_csv('company_data.csv',index = False)
显示数据框时,结果看起来很好。enter image description here 但是在打开 csv 文件时,列并不像预期的那样。 enter image description here谁能告诉我原因。
【问题讨论】:
-
这是 Excel 的问题。尝试通过数据/新查询/来自文件/来自 CSV。
-
@divingTobi 谢谢。它现在正在工作。有什么方法可以直接打开吗?
-
不,不知道有什么办法。但是为什么不将文件保存为 xlsx 开始呢?
df.to_excel('file.xlsx'). -
是的,会这样做。谢谢!
标签: python pandas csv export-to-csv