如何使用PRAW在Python中使用惰性对象进行尝试?

时间:2014-01-20 03:08:52

标签: python praw

我想知道是否有人可以协助我查看“get_redditor”是否返回错误。我使用了“fetch = True”参数,它仍然返回。但是,如果您转到用户“Alaska88”页面,则它不存在。当程序到达“for comment in comments”行并且我假设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行,in            评论中的评论:文件“/usr/local/lib/python2.7/dist-packages/praw-

然后错误在此处结束=>

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

1 个答案:

答案 0 :(得分:3)

你最有可能抓住错误的例外。

urllib2.HTTPError在你的除外,但requests.exceptions.HTTPError在你的追溯中。