Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #tob100sub.py
- """
- tob100sub is a program that gets a list of top 100 subreddit on reddit.com and
- opens the a random subreddit from the list on a new tab on your default browser.
- """
- # python imports
- import random
- import urllib2
- import webbrowser
- import random
- import re
- from BeautifulSoup import BeautifulSoup
- site_name = 'http://www.redditlist.com/'
- def get_content():
- req = urllib2.Request(site_name)
- site_data = urllib2.urlopen(req)
- content = BeautifulSoup(site_data)
- return content
- def get_subreddits(site_content):
- subreddits = []
- table_reddits = site_content.findAll('tr', {'class': re.compile("halluc (?:bgcol)?")})
- for i in range(100):
- name = str(table_reddits[i].find('td', {'class': 'redditname'}).a.string)
- subreddits.append(name)
- return subreddits
- def get_random(subreddits):
- randvalue = random.randrange(100)
- reddit_name = subreddits[randvalue].lower()
- return reddit_name
- def open_page(reddit_name):
- baseSite = "http://www.reddit.com/r/"
- redditSite = baseSite + reddit_name
- print "Opening: %s" % (redditSite)
- webbrowser.open_new_tab(redditSite)
- def main():
- site_content = get_content()
- subreddits = get_subreddits(site_content)
- reddit_name = get_random(subreddits)
- open_page(reddit_name)
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment