__ __ _ __ ____
/\ \/\ \/\`'__\/',__\
\ \ \_\ \ \ \//\__, `\
\ \____/\ \_\\/\____/
\/___/ \/_/ \/___/... Universal Reddit Scraper
- Contact
- Introduction
- URS Overview
- Contributing
- Contributors
- Derivative Projects
- Supplemental Documents
Whether you are using URS for enterprise or personal use, I am very interested in hearing about your use case and how it has helped you achieve a goal.
Additionally, please send me an email if you would like to contribute, have questions, or want to share something you have built on top of it.
You can send me an email or leave a note by clicking on either of these badges. I look forward to hearing from you!
This is a comprehensive Reddit scraping tool that integrates multiple features:
- Scrape Reddit via
PRAW
(the official Python Reddit API Wrapper)- Scrape Subreddits
- Scrape Redditors
- Scrape submission comments
- Analytical tools for scraped data
- Get frequencies for words that are found in submission titles, bodies, and/or comments
- Generate a wordcloud from scrape results
Run pip install -r requirements.txt
to get all project dependencies.
You will need your own Reddit account and API credentials for PRAW. See the Getting Started section for more information.
NOTE: PRAW
is currently supported on Python 3.6+.
All files except for those generated by the wordcloud tool are exported to JSON by default. Wordcloud files are exported to PNG by default. URS supports exporting to CSV as well, but JSON is the more versatile option. See the Exporting section for more information.
All exported files are saved within the scrapes
directory and stored in a sub-directory labeled with the date. Many more sub-directories may be created in the date directory. Sub-directories are only created when its respective tool is run. For example, if you only use the Subreddit scraper, only the subreddits
directory is created.
The subreddits
, redditors
, or comments
directories are created when you run each scraper.
The analytics
directory is created when you run any of the analytical tools. Within it, the frequencies
or wordclouds
directories are created when you run each tool. See the Analytical Tools section for more information.
This is the samples' directory structure generated by the tree command.
scrapes/
└── 02-24-2021
├── analytics
│ ├── frequencies
│ │ ├── askreddit-hot-100-results.csv
│ │ └── askreddit-hot-100-results.json
│ └── wordclouds
│ ├── cscareerquestions-search-'job'-past-year.png
│ └── What do you think about adults that don’t eat ve---RAW.png
├── comments
│ ├── What do you think about adults that don’t eat ve---50-results.json
│ └── What do you think about adults that don’t eat ve---RAW.json
├── redditors
│ └── spez-5-results.json
├── subreddits
│ ├── askreddit-hot-100-results.csv
│ ├── askreddit-hot-100-results.json
│ ├── cscareerquestions-search-'job'-past-year.json
│ └── wallstreetbets-top-5-results-past-month-rules.json
└── urs.log
Scrape speed is determined by a couple things:
- The number of results returned for Subreddit or Redditor scraping
- The submission's popularity (total number of comments) for submission comments scraping
- Your internet connection speed
It is very quick and easy to get Reddit API credentials. Refer to my guide to get your credentials, then update the API
dictionary located in Credentials.py
Yes, PRAW has rate limits. These limits are proportional to how much karma you have accumulated - the higher the karma, the higher the rate limit. This has been implemented to mitigate spammers and bots that utilize PRAW.
Rate limit information for your account is displayed in a small table underneath the successful login message each time you run any of the PRAW scrapers. I have also added a --check
flag if you want to quickly view this information.
URS will display an error message as well as the rate limit reset date if you have used all your available requests.
There are a couple ways to go about solving issues with rate limits:
- Scrape intermittently
- Use an account with high karma to get your PRAW credentials
- Scrape less results per run
Available requests are refilled if you use the PRAW scrapers intermittently, which might be a good solution for bypassing rate limit issues. This can be especially helpful if you have automated URS and are not looking at the output on each run.
These attributes are included in each scrape.
Subreddits | Redditors | Submission Comments |
---|---|---|
Title | Name | Parent ID |
Flair | Fullname | Comment ID |
Date Created | ID | Author |
Upvotes | Date Created | Date Created |
Upvote Ratio | Comment Karma | Upvotes |
ID | Link Karma | Text |
Is Locked? | Is Employee? | Edited? |
NSFW? | Is Friend? | Is Submitter? |
Is Spoiler? | Is Mod? | Stickied? |
Stickied? | Is Gold? | |
URL | *Submissions | |
Comment Count | *Comments | |
Text | *Hot | |
*New | ||
*Controversial | ||
*Top | ||
*Upvoted (may be forbidden) | ||
*Downvoted (may be forbidden) | ||
*Gilded | ||
*Gildings (may be forbidden) | ||
*Hidden (may be forbidden) | ||
*Saved (may be forbidden) |
*Includes additional attributes; see Redditors section for more information.
*This GIF is uncut.
Usage: $ ./Urs.py -r SUBREDDIT (H|N|C|T|R|S) N_RESULTS_OR_KEYWORDS
Supported export formats: JSON and CSV. To export to CSV, include the --csv
flag.
You can specify Subreddits, the submission category, and how many results are returned from each scrape. I have also added a search option where you can search for keywords within a Subreddit.
These are the submission categories:
- Hot
- New
- Controversial
- Top
- Rising
- Search
The file names for all categories except for Search will follow this format:
"[SUBREDDIT]-[POST_CATEGORY]-[N_RESULTS]-result(s).[FILE_FORMAT]"
If you searched for keywords, file names will follow this format:
"[SUBREDDIT]-Search-'[KEYWORDS]'.[FILE_FORMAT]"
Time filters may be applied to some categories. Here is a table of the categories on which you can apply a time filter as well as the valid time filters.
Categories | Time Filters |
---|---|
Controversial | All (default) |
Top | Day |
Search | Hour |
Month | |
Week | |
Year |
Specify the time filter after the number of results returned or keywords you want to search for.
Usage: $ ./Urs.py -r SUBREDDIT (C|T|S) N_RESULTS_OR_KEYWORDS OPTIONAL_TIME_FILTER
If no time filter is specified, the default time filter all
is applied. The Subreddit settings table will display None
for categories that do not offer the additional time filter option.
If you specified a time filter, -past-[TIME_FILTER]
will be appended to the file name before the file format like so:
"[SUBREDDIT]-[POST_CATEGORY]-[N_RESULTS]-result(s)-past-[TIME_FILTER].[FILE_FORMAT]"
Or if you searched for keywords:
"[SUBREDDIT]-Search-'[KEYWORDS]'-past-[TIME_FILTER].[FILE_FORMAT]"
You can also include the Subreddit's rules and post requirements in your scrape data by including the --rules
flag. This only works when exporting to JSON.
If rules are included in your file, -rules
will be appended to the end of the file name.
After submitting the arguments and Reddit validation, URS will display a table of Subreddit scraping settings as a final check before executing. You can include the -y
flag to bypass this and immediately scrape.
Exported files will be saved to the subreddits
directory.
NOTE: Up to 100 results are returned if you search for keywords within a Subreddit. You will not be able to specify how many results to keep.
*This GIF has been cut for demonstration purposes.
Usage: $ ./Urs.py -u REDDITOR N_RESULTS
Supported export formats: JSON.
You can also scrape Redditor profiles and specify how many results are returned.
Some Redditor attributes are sorted differently. Here is a table of how each is sorted.
Attribute Name | Sorted By/Time Filter |
---|---|
Comments | Sorted By: New |
Controversial | Time Filter: All |
Gilded | Sorted By: New |
Hot | Determined by other Redditors' interactions |
New | Sorted By: New |
Submissions | Sorted By: New |
Top | Time Filter: All |
Of these Redditor attributes, the following will include additional attributes:
Submissions, Hot, New, Controversial, Top, Upvoted, Downvoted, Gilded, Gildings, Hidden, and Saved | Comments |
---|---|
Title | Date Created |
Date Created | Score |
Upvotes | Text |
Upvote Ratio | Parent ID |
ID | Link ID |
NSFW? | Edited? |
Text | Stickied? |
Replying to (title of submission or comment) | |
In Subreddit (Subreddit name) |
The file names will follow this format:
"[USERNAME]-[N_RESULTS]-result(s).json"
Exported files will be saved to the redditors
directory.
NOTE: If you are not allowed to access a Redditor's lists, PRAW will raise a 403 HTTP Forbidden exception and the program will just append "FORBIDDEN"
underneath that section in the exported file.
NOTE: The number of results returned are applied to all attributes. I have not implemented code to allow users to specify different number of results returned for individual attributes.
*These GIFs have been cut for demonstration purposes.
Usage: $ ./Urs.py -c SUBMISSION_URL N_RESULTS
Supported export formats: JSON.
You can also scrape comments from submissions and specify the number of results returned.
Comments are sorted by "Best", which is the default sorting option when you visit a submission.
There are two ways you can scrape comments: structured or raw. This is determined by the number you pass into N_RESULTS
:
Scrape Type | N_RESULTS |
---|---|
Structured | N_RESULTS >= 1 |
Raw | N_RESULTS = 0 |
Structured scrapes resemble comment threads on Reddit and will include down to third-level comment replies.
Raw scrapes do not resemble comment threads, but returns all comments on a submission in level order: all top-level comments are listed first, followed by all second-level comments, then third, etc.
Of all scrapers included in this program, this usually takes the longest to execute. PRAW returns submission comments in level order, which means scrape speeds are proportional to the submission's popularity.
The file names will follow this format:
"[POST_TITLE]-[N_RESULTS]-result(s).json"
Exported files will be saved to the comments
directory.
NOTE: You cannot specify the number of raw comments returned. The program with scrape all comments from the submission.
This suite of tools can be used after scraping data from Reddit. Both of these tools analyze the frequencies of words found in submission titles and bodies, or comments within JSON scrape data.
There are a few ways you can quickly get the correct filepath to the scrape file:
- Drag and drop the file into the terminal.
- Partially type the path and rely on tab completion support to finish the full path for you.
Running either tool will create the analytics
directory within the date directory. This directory is located in the same directory in which the scrape data resides. For example, if you run the frequencies generator on February 16th for scrape data that was captured on February 14th, analytics
will be created in the February 14th directory. Command history will still be written in the February 16th urs.log
.
A shortened export path is displayed once URS has completed exporting the data, informing you where the file is saved within the scrapes
directory. You can open urs.log
to view the full path.
The sub-directories frequencies
or wordclouds
are created in analytics
depending on which tool is run.
NOTE: Do not move the scrapes
directory elsewhere if you want to use these tools. URS uses a relative path to save the generated files.
The data varies depending on the scraper, so these tools target different fields for each type of scrape data:
Scrape Data | Targets |
---|---|
Subreddit | "title", "text" |
Redditor | "title", "body", "text" |
Submission Comments | "text" |
For Subreddit scrapes, data is pulled from the "title" and "text" fields for each submission (submission title and body).
For Redditor scrapes, data is pulled from all three fields because both submission and comment data is returned. The "title" and "body" fields are targeted for submissions, and the "text" field is targeted for comments.
For submission comments scrapes, data is only pulled from the "text" field of each comment.
File names are identical to the original scrape data so that it is easier to distinguish which analytical file corresponds to which scrape.
*This GIF is uncut.
Usage: $ ./Urs.py -f FILE_PATH
Supported export formats: JSON and CSV. To export to CSV, include the --csv
flag.
You can generate a dictionary of word frequencies created from the words within the target fields.
Frequencies export to JSON by default, but this tool also works well in CSV format.
Exported files will be saved to the analytics/frequencies
directory.
*This GIF is uncut.
Usage: $ ./Urs.py -wc FILE_PATH
Supported export formats: .eps, .jpeg, .jpg, .pdf, .png (default), .ps, .rgba, .tif, .tiff.
Taking word frequencies to the next level, you can generate wordclouds based on word frequencies. This tool is independent of the frequencies generator - you do not need to run the frequencies generator before creating a wordcloud.
PNG is the default format, but you can also export to any of the options listed above by including the format as the second flag argument.
Usage: $ ./Urs.py -wc FILE_PATH OPTIONAL_EXPORT_FORMAT
Exported files will be saved to the analytics/wordclouds
directory.
Wordclouds are saved to file by default. If you do not want to keep a file, include the --nosave
flag to only display the wordcloud.
As stated before, URS supports exporting to either JSON or CSV. JSON is the default format - you will have to include the --csv
flag to export to CSV.
I recommend only exporting to CSV when using:
- The Subreddit scraper
- The word frequencies generator
These tools are also suitable for CSV format and are optimized to do so if you want to use that format instead.
JSON is the more practical option for Redditor and submission comments scraping, which is why I have designed these scrapers to work best in this format. It is much easier to read the scrape results since Redditor scraping returns attributes that include additional submission or comment attributes.
Comments scraping is especially easier to read in JSON format because structured exports look similar to threads on Reddit. You can process all the information pertaining to a comment much quicker compared to CSV.
You can still export Redditor data and submission comments to CSV, but you will be disappointed with the results.
See the samples for scrapes ran on February 24, 2021.
See the Contact section for ways to reach me.
Consider the scope of this project before submitting a pull or feature request. URS stands for Universal Reddit Scraper. Two important aspects are listed in its name - universal and scraper.
I will not approve feature or pull requests that deviate from its sole purpose. This may include scraping a specific aspect of Reddit or adding functionality that allows you to post a comment with URS. Adding either of these requests will no longer allow URS to be universal or merely a scraper. However, I am more than happy to approve requests that enhance the current scraping capabilities of URS.
Although I will not approve requests that deviate from the project scope, feel free to reach out if you have built something on top of URS or have made modifications to scrape something specific on Reddit. I will add your project to the Derivative Projects section!
You can suggest new features or changes by going to the Issues tab and fill out the Feature Request template. If there is a good reason for a new feature, I will consider adding it.
You are also more than welcome to create a pull request - adding additional features, improving runtime, or refactoring existing code. If it is approved, I will merge the pull request into the master branch and credit you for contributing to this project.
Date | User | Contribution |
---|---|---|
March 11, 2020 | ThereGoesMySanity | Created a pull request adding 2FA information to README |
October 6, 2020 | LukeDSchenk | Created a pull request fixing "[Errno 36] File name too long" issue, making it impossible to save comment scrapes with long titles |
October 10, 2020 | IceBerge421 | Created a pull request fixing a cloning error occuring on Windows machines due to illegal file name characters, " , found in two scrape samples |
This is a showcase for projects that are built on top of URS!