我正在使用下面的脚本获取一个品牌的谷歌新闻结果。但是,我希望能够将结果导出到CSV或Excel文件。有人能帮我吗
pip install pygooglenews --upgrade
from pygooglenews import GoogleNews
gn = GoogleNews(country = 'IE')
def get_titles(search):
stories = []
search = gn.search(search)
newsitem = search['entries']
for item in newsitem:
story = {
'title' : item.title,
'link' : item.link
}
stories.append(story)
return stories
print(get_titles('aldi'))
你可以试试下面的方法
以dict列表的形式获取数据,循环列表并将每个条目写入文件
stories.csv
您可以使用CSV模块在从搜索中提取行时写入这些行
一种简单(但繁重)的方法是使用pandas,它可以轻松地将字典列表转换为数据帧
这里回答了内置方式:How do I convert this list of dictionaries to a csv file?
相关问题 更多 >
编程相关推荐