我是Python新手,我正试图使用PyGoogleNews库为一个项目从Google中提取被刮掉的标题。我在用谷歌Colab。PyGoogleNews代码正在为我完美地运行,我对此感到满意。代码正在创建csv,但是我无法用刮掉的标题结果填充csv。我希望将被刮掉的标题输出导出到csv文件中,因为我将执行情感分析并下载/提取它来对其执行进一步的分析。我会非常感谢任何帮助,因为这已经困扰了我几天,我相信这是非常明显的事情!提前谢谢你。
!pip install pygooglenews --upgrade
import pandas as pd
import csv
from pygooglenews import GoogleNews
gn = GoogleNews (lang = 'en', country = 'UK')
Xmassearch = gn.search('intitle:Christmas', helper = True, from_ = '2019-12-01', to_= '2019-12-31')
print(Xmassearch['feed'].title)
for item in Xmassearch ['entries']:
print(item['title'])
file = open("Christmassearch.csv", "w")
writer = csv.writer(file)
writer.writerow(["Xmassearch"])
file.close()
发布于 2021-12-19 02:14:28
我没有GoogleNews,我找不到任何使用关键字entries
的文档,所以我不能确切地说明它应该如何工作,所以.
如果迭代Xmassearch['entries']
对您有效,那么将迭代向下移到打开CSV并开始编写的地方。
我还重写/结构化了您的文件处理,以使用Python的with
复合语句,这样您就不必管理文件的关闭:
with open('Christmassearch.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(['Xmassearch']) # I presume you meant this to be your header
# Use your loop from before...
for item in Xmassearch ['entries']:
# And write each item
writer.writerow([item['title']])
https://stackoverflow.com/questions/70407985
复制相似问题