In praw, I am trying to print the body of a comment, but what if I came across an empty comment?

I am trying to print all the comments from the top subreddit posts so that my bot can parse them. I had this before, but I tried to run it now, and I ran into an error.

Here is my code:

r = praw.Reddit('Comment crawler v1.0 by /u/...') r.login('username', 'password') subreddit=r.get_subreddit('subreddit') post_limit = 25 subreddit_posts = subreddit.get_hot(limit=post_limit) subids = set() for submission in subreddit_posts: subids.add(submission.id) subid = list(subids) i=0 while i < post_limit: submission = r.get_submission(submission_id=subid[i]) flat_comments = praw.helpers.flatten_tree(submission.comments) with open('alreadydone.txt', 'r') as f: already_done = [line.strip() for line in f] f.close() for comment in flat_comments: if "Cricketbot, give me Australian news" in **comment.body** and comment.id not in already_done: info = feedparser.parse(Australia) #Australia gives a link to an RSS feed. 

The split section is where I ran into the problem. I'm trying to look at the comments that say "Cricketbot, give me Australian news." Unfortunately, if the comment body does not exist, i.e. The comment is empty, the code returns an attribute error and says that the comment does not have the body attribute.

How to get around this problem?

To view all the code, go to the GitHub page for the bot .

+6
source share
1 answer

This usually helps to add a stack so that people can see the actual error. However, as a PRAW developer, I know that the error is something like MoreComments type has no attribute body .

There are three easy ways to solve your problem. First, just wrap the if "Cricketbot" in try / except and ignore the attribute error.

 try: if "Cricketbot..." ... except AttributeError: pass 

This is not terribly interesting. The second way is to make sure that you are actually working with an object with the body attribute, which can be done in two ways:

First, you need to explicitly check if the attribute exists:

 for comment in flat_comments: if not hasattr(comment, 'body'): continue ... 

Secondly, make sure that you are actually working with Comment objects, not with the MoreComments object:

 for comment in flat_comments: if not isinstance(comment, praw.objects.Comment): continue ... 

However, when you run any of the above solutions, you actually do not process all the comments for the view, since you are missing everything that is hidden behind the MoreComments [ ref ] object. To replace the MoreComments object MoreComments some (replacing all of them can be very inefficient) comments require the use of the replace_more_comments function before aligning the tree:

 submission = r.get_submission(submission_id=subid[i]) submission.replace_more_comments(limit=16, threshold=10) flat_comments = praw.helpers.flatten_tree(submission.comments) 

Setting limit=16 and threshold=10 means making no more than 16 additional requests and only making requests that will lead to at least 10 additional comments. You can play with these values ​​as you wish, but note that for each replacement an additional request is required (2 seconds), and some of them give only one comment.

I hope this helps.

+13
source

Source: https://habr.com/ru/post/948563/


All Articles