Warm tip: This article is reproduced from serverfault.com, please click

Praw Reddit Web Crawler Error "object has no attribute"

发布于 2020-12-29 17:18:55

I'm trying to use a Reddit Web Crawler to extract top comments from specific subreddits and save them in a .csv-File.

Here is my code:

import datetime
import praw
import pandas as pd
reddit = praw.Reddit(client_id='',
                 client_secret='',
                 password='',
                 user_agent='',
                 username='')

#Reddit Crawler
list_counter = 0
sr_list = ['politics', 'conservative', 'liberal', 'libertarian', 'donaldtrump', 'joebiden', 'democrats', 'republican']
data = []
comments = []

while list_counter < len(sr_list):
subreddit = reddit.subreddit(sr_list[list_counter]).top(time_filter='month', limit=1) #Change limit for number of threads, .new/.hot
for s in subreddit:  # for every submission in the subreddit
    # fetch top level comments
    for c in s.comments:
        c_time = datetime.datetime.fromtimestamp(c.created_utc) #Convert format of comment time (Y:M:D , H:M:S)
        comments.append([c.subreddit, c._submission, 'Comment', s.title, c.author, c_time, c.score, c.body])
            #May not need c._submission
    s_time = datetime.datetime.fromtimestamp(s.created_utc) #Convert format of threadRE time
    data.append([s.subreddit, s.id,'Thread', s.title, s.author, s_time, s.score, s.selftext])
list_counter+= 1

#Export to CSV#
df = pd.DataFrame(data, columns=['Subreddit Name','Thread ID', 'Thread/Comment', 'Thread Title', 'Author',
                             'Timestamp','Score','Content'])
df1 = pd.DataFrame(comments, columns=['Subreddit Name','Thread ID','Thread/Comment', 'Thread Title', 'Author',
                             'Timestamp','Score','Content'])
result = pd.concat([df, df1])
result.to_csv('Raw Data.csv', index=False)

The code works fine most of the time but with larger quanities of posts and comments it gives back this error message:

Traceback (most recent call last):
   File "/Users/robin/Documents/Python/code/Jonckr Reddit Web Crawler.py", line 23, in <module>
      c_time = datetime.datetime.fromtimestamp(c.created_utc) #Convert format of comment time (Y:M:D , H:M:S)
AttributeError: 'MoreComments' object has no attribute 'created_utc'

Process finished with exit code 1

I'm pretty much an amateur at programming so I don't know how to solve this issue. Help would be much appreciated.

Thank you in advance.

Questioner
Tony Montana
Viewed
0
Benjamin Rowell 2020-12-30 01:27:12

In the Praw docs it states that these MoreComments objects are a representation of the load more comments and continue this thread links encountered on Reddit.

To get around it, they suggest the following:

from praw.models import MoreComments

for top_level_comment in submission.comments:
    if isinstance(top_level_comment, MoreComments):
        continue

In the context of your code, try the following:

for c in s.comments:
    if isinstance(c, MoreComments):
        continue
    c_time = datetime.datetime.fromtimestamp(c.created_utc) #Convert format of comment time (Y:M:D , H:M:S)
    ...