python - 如何使用 PRAW 对 Python 中的惰性对象执行 try- except ?

标签 python praw

我想知道是否有人可以协助我检查“get_redditor”是否返回错误。我已经使用了“fetch=True”参数,它仍然返回。但是,如果您转到用户“Alaska88”页面,则它不存在。当程序到达“注释中的注释”行时会发生错误,并且我假设 try- except 由于它是一个惰性对象而不起作用。预先感谢您的任何时间或帮助。

import praw
import urllib2
r = praw.Reddit('testing scraper')
r.login()
account = r.get_redditor('Alaska88',fetch=True)
comments = account.get_comments(sort ='new',time ='all')        
print 'before comment loop'
try:
        for comment in comments:
                print 'in comment loop' 
                print(comment.body.encode('utf-8'))
        print('/////////////////////////')
except urllib2.HTTPError:
        print 'In Except'       
        time.sleep(60)
        pass 

错误从这里开始 =>

文件“reddit_bot.py”,第 9 行,位于 对于评论中的评论:文件“/usr/local/lib/python2.7/dist-packages/praw-

错误就在这里结束=>

引发 HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 404 客户端错误: 未找到

最佳答案

您很可能捕获了错误的异常。

urllib2.HTTPError 位于您的 except 中,但 requests.exceptions.HTTPError 位于您的回溯中。

关于python - 如何使用 PRAW 对 Python 中的惰性对象执行 try- except ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21225642/

相关文章:

python - PRAW Subreddit 模型对于一些请求工作正常 - 然后只发送一段时间 503 错误

python - 将数组作为命令行参数传递给 python 脚本

python - PRAW:使用 OAuth 授权会阻止我获得提交/评论

python - pickle 后文件大小急剧增加

python - 具有无循环移位操作的二进制掩码

python - 不和谐、粗暴、阻塞

python - 在 Flask 中打印列表

python-3.x - 无法使用 PRAW 从某些 Reddit 子版 block 中获取随机帖子

python - 为什么[:1] and [0] do not generate the same result?

python - 在 python 脚本上附加 MIME 时出错