不久前我可以使用 oauth2 成功连接到 reddit 的服务器,但是当刚才运行我的脚本时,我收到一个 KeyError
,然后是一个 NoSectionError
。下面的代码后面是异常,(代码已缩减至其要点)。
import praw
# Configuration
APP_UA = 'useragent'
...
...
...
r = praw.Reddit(APP_UA)
错误消息:
Traceback (most recent call last):
File "D:\Directory\Python\lib\configparser.py", line 843, in items
d.update(self._sections[section])
KeyError: 'useragent'
A NoSectionError occurred when handling the above exception.
"During handling of the above exception, another exception occurred:"
'Traceback (most recent call last):
File "D:\Directory\Python\Projects\myprj for Reddit, globaloffensive\oddshotcrawler.py", line 19, in <module>
r = praw.Reddit(APP_UA)
File "D:\Directory\Python\lib\site-packages\praw\reddit.py", line 84, in __init__
**config_settings)
File "D:\Directory\Python\lib\site-packages\praw\config.py", line 47, in __init__
raw = dict(Config.CONFIG.items(site_name), **settings)
File "D:\Directory\Python\lib\configparser.py", line 846, in items
raise NoSectionError(section)
configparser.NoSectionError: No section: 'useragent'
[Finished in 0.2s]
最佳答案
尝试给它一个 user_agent kwarg。
r = praw.Reddit(useragent=APP_UA)
关于python - 将 '' praw.Reddit'' 分配给变量时出现 KeyError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36361826/