|
| 1 | +import requests |
| 2 | +import csv |
| 3 | +import time |
| 4 | +import sqlite3 |
| 5 | +from bs4 import BeautifulSoup |
| 6 | + |
| 7 | + |
| 8 | +def sql_connection(): |
| 9 | + """ |
| 10 | + Establishes a connection to the SQL file database |
| 11 | + :return connection object: |
| 12 | + """ |
| 13 | + con = sqlite3.connect('SubredditDatabase.db') |
| 14 | + return con |
| 15 | + |
| 16 | + |
| 17 | +def sql_table(con): |
| 18 | + """ |
| 19 | + Creates a table in the database (if it does not exist already) |
| 20 | + to store the tweet info |
| 21 | + :param con: |
| 22 | + :return: |
| 23 | + """ |
| 24 | + cur = con.cursor() |
| 25 | + cur.execute("CREATE TABLE IF NOT EXISTS posts(SUBREDDIT text, TAG text, " |
| 26 | + " TITLE text, AUTHOR text, TIMESTAMP text, UPVOTES int, " |
| 27 | + " COMMENTS text, URL text)") |
| 28 | + con.commit() |
| 29 | + |
| 30 | + |
| 31 | +def sql_insert_table(con, entities): |
| 32 | + """ |
| 33 | + Inserts the desired data into the table to store tweet info |
| 34 | + :param con: |
| 35 | + :param entities: |
| 36 | + :return: |
| 37 | + """ |
| 38 | + cur = con.cursor() |
| 39 | + cur.execute('INSERT INTO posts(SUBREDDIT, TAG, TITLE, AUTHOR, ' |
| 40 | + 'TIMESTAMP, UPVOTES, COMMENTS, URL) ' |
| 41 | + 'VALUES(?, ?, ?, ?, ?, ?, ?, ?)', entities) |
| 42 | + con.commit() |
| 43 | + |
| 44 | + |
| 45 | +def scraper(): |
| 46 | + """ |
| 47 | + The function scrapes the post info from the desired subreddit and stores it |
| 48 | + into the desired file. |
| 49 | + :return: |
| 50 | + """ |
| 51 | + con = sql_connection() |
| 52 | + sql_table(con) |
| 53 | + |
| 54 | + while 1: |
| 55 | + subreddit = input('\n\nEnter the name of the subreddit: r/').lower() |
| 56 | + max_count = int(input('Enter the maximum number of entries to collect: ')) |
| 57 | + select = int(input('Select tags to add for the search: \n1. hot\n2. new' |
| 58 | + '\n3. rising\n4. controversial\n5. top\nMake your choice: ')) |
| 59 | + |
| 60 | + if select == 1: |
| 61 | + tag = 'hot' |
| 62 | + tag_url = '/' |
| 63 | + elif select == 2: |
| 64 | + tag = 'new' |
| 65 | + tag_url = '/new/' |
| 66 | + elif select == 3: |
| 67 | + tag = 'rising' |
| 68 | + tag_url = '/rising/' |
| 69 | + elif select == 4: |
| 70 | + tag = 'controversial' |
| 71 | + tag_url = '/controversial/' |
| 72 | + elif select == 5: |
| 73 | + tag = 'top' |
| 74 | + tag_url = '/top/' |
| 75 | + |
| 76 | + # URL for the desired subreddit |
| 77 | + url = 'https://old.reddit.com/r/' + subreddit |
| 78 | + |
| 79 | + # Using a user-agent to mimic browser activity |
| 80 | + headers = {'User-Agent': 'Mozilla/5.0'} |
| 81 | + |
| 82 | + req = requests.get(url, headers=headers) |
| 83 | + |
| 84 | + if req.status_code == 200: |
| 85 | + soup = BeautifulSoup(req.text, 'html.parser') |
| 86 | + print(f'\nCOLLECTING INFORMATION FOR r/{subreddit}....') |
| 87 | + |
| 88 | + attrs = {'class': 'thing'} |
| 89 | + counter = 1 |
| 90 | + full = 0 |
| 91 | + reddit_info = [] |
| 92 | + while 1: |
| 93 | + for post in soup.find_all('div', attrs=attrs): |
| 94 | + try: |
| 95 | + # To obtain the post title |
| 96 | + title = post.find('a', class_='title').text |
| 97 | + |
| 98 | + # To get the username of the post author |
| 99 | + author = post.find('a', class_='author').text |
| 100 | + |
| 101 | + # To obtain the time of the post |
| 102 | + time_stamp = post.time.attrs['title'] |
| 103 | + |
| 104 | + # To obtain the number of comments on the post |
| 105 | + comments = post.find('a', class_='comments').text.split()[0] |
| 106 | + if comments == 'comment': |
| 107 | + comments = 0 |
| 108 | + |
| 109 | + # To get the number of comments on the post |
| 110 | + upvotes = post.find('div', class_='score likes').text |
| 111 | + if upvotes == '•': |
| 112 | + upvotes = "None" |
| 113 | + |
| 114 | + # To get the URL of the post |
| 115 | + link = post.find('a', class_='title')['href'] |
| 116 | + link = 'www.reddit.com' + link |
| 117 | + |
| 118 | + # Entering all the collected information into our database |
| 119 | + entities = (subreddit, tag, title, author, time_stamp, upvotes, |
| 120 | + comments, link) |
| 121 | + sql_insert_table(con, entities) |
| 122 | + |
| 123 | + if counter == max_count: |
| 124 | + full = 1 |
| 125 | + break |
| 126 | + |
| 127 | + counter += 1 |
| 128 | + except AttributeError: |
| 129 | + continue |
| 130 | + |
| 131 | + if full: |
| 132 | + break |
| 133 | + |
| 134 | + try: |
| 135 | + # To go to the next page |
| 136 | + next_button = soup.find('span', class_='next-button') |
| 137 | + next_page_link = next_button.find('a').attrs['href'] |
| 138 | + |
| 139 | + time.sleep(2) |
| 140 | + |
| 141 | + req = requests.get(next_page_link, headers=headers) |
| 142 | + soup = BeautifulSoup(req.text, 'html.parser') |
| 143 | + except: |
| 144 | + break |
| 145 | + |
| 146 | + print('DONE!\n') |
| 147 | + ans = input('Press (y) to continue or any other key to exit: ').lower() |
| 148 | + if ans == 'y': |
| 149 | + continue |
| 150 | + else: |
| 151 | + print('Exiting..') |
| 152 | + break |
| 153 | + else: |
| 154 | + print('Error fetching results.. Try again!') |
| 155 | + |
| 156 | + |
| 157 | +if __name__ == '__main__': |
| 158 | + scraper() |
| 159 | + |
0 commit comments