此代码应该获取有关帖子的一些信息,例如标题、赞成票等。然后将其写入 CSV 文件。但是当它运行时我得到这个错误:
Traceback (most recent call last):
File "main.py", line 71, in <module>
writer.writerow(data)
File "/usr/lib/python3.8/csv.py", line 154, in writerow
return self.writer.writerow(self._dict_to_list(rowdict))
File "/usr/lib/python3.8/csv.py", line 151, in <genexpr>
return (rowdict.get(key, self.restval) for key in self.fieldnames)
AttributeError: 'set' object has no attribute 'get'
这是代码:
import praw
import random
import time
import csv
#from keep_Aliv import keep_alive
username = "XXXXXX"
#keep_alive()
print('AAAAAAA')
reddit = praw.Reddit(client_id="XXXXXX",
client_secret="XXXXXX",
user_agent="bot",
username=username,
password='XXXXXX')
subreddit = reddit.subreddit('all')
with open('pdata.csv', 'a') as f:
headers = [
'ID', 'Date_utc', 'Upvotes', 'Number of Comments', 'Subthread name'
]
writer = csv.DictWriter(f,
fieldnames=headers,
extrasaction='ignore',
dialect='excel')
writer.writeheader()
for post in subreddit.stream.submissions():
#print(post.title)
data = {
"ID": post.id,
"Date_utc": post.created_utc,
"Upvotes": post.ups,
"Number of comments": post.num_comments,
"Subthread name": post.title,
}
writer.writerow(data)
print(data)
好的,那我应该如何解决这个问题?请告诉是否需要一些其他信息。
谢谢 :)
(主要问题现在已解决,编译器现在没有抛出错误,但另一方面,程序没有在 csv 文件中写入任何内容。编译器没有检测到任何错误,但它仍然没有做什么它应该是。我该如何解决?顺便说一下帖子中的代码是工作代码)