Last active
December 13, 2021 09:15
-
-
Save khaledadrani/a661591844b4308aeaefbbd49952641b to your computer and use it in GitHub Desktop.
starter code for praw python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for post in subreddit.hot(limit=5): | |
print(post.title) | |
print() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
posts = subreddit.top("month") | |
# Scraping the top posts of the current month | |
posts_dict = {"Title": [], "Post Text": [], | |
"ID": [], "Score": [], | |
"Total Comments": [], "Post URL": [] | |
} | |
for post in posts: | |
# Title of each post | |
if post.link_flair_text == "Industry News": | |
posts_dict["Title"].append(post.title) | |
# Text inside a post | |
posts_dict["Post Text"].append(post.selftext) | |
# Unique ID of each post | |
posts_dict["ID"].append(post.id) | |
# The score of a post | |
posts_dict["Score"].append(post.score) | |
# Total number of comments inside the post | |
posts_dict["Total Comments"].append(post.num_comments) | |
# URL of each post | |
posts_dict["Post URL"].append(post.url) | |
# Saving the data in a pandas dataframe | |
top_posts = pd.DataFrame(posts_dict) | |
top_posts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment