Last active
January 11, 2019 23:12
-
-
Save khayyamsaleem/77c3c68d94cb926a17c9ef696be35e5b to your computer and use it in GitHub Desktop.
gets all post titles on the front page of a subreddit
This file contains hidden or 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 requests | |
from bs4 import BeautifulSoup | |
headers = { | |
'User-Agent' : 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.63' | |
} | |
subreddit = input("Enter a sub-reddit name: ").strip().lower() | |
URL = "https://www.reddit.com/r/%s" % subreddit | |
page = requests.get(URL, headers=headers) | |
if page: | |
print("-----------------------------\nALL POST TITLES ON FRONT-PAGE\n-----------------------------") | |
soup = BeautifulSoup(page.text, "html.parser") | |
for post_title in soup.find_all('h2'): | |
print(post_title.text) | |
else: | |
print("PAGE NOT FOUND") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment