-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredditparser.py
More file actions
51 lines (35 loc) · 1.17 KB
/
Copy pathredditparser.py
File metadata and controls
51 lines (35 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import urllib.request
import re
class Downloader():
'''
Class to retreive HTML code and binary files from a specific website
'''
def __init__(self,url):
self.url = url
self.contents = ''
def download(self):
browser = urllib.request.urlopen(self.url)
response = browser.getcode()
if response == 200:
self.contents = browser.read().decode("utf-8")
class redditParser(Downloader):
'''
Class for parsing usernames from xkcd.com
'''
def __init__(self,url):
Downloader.__init__(self,url)
self.users = []
def get_last_username(self):
try:
#self.last_username = re.search(r'"http://www.reddit.com/user/(\w+)"', self.contents).group(1)
self.users = re.findall(r'"http://www.reddit.com/user/(\w+)"', self.contents)
except:
self.users = "-5"
def get_current_user(self):
self.download()
self.get_last_username()
if __name__ == '__main__':
url = "http://www.reddit.com/"
reddit_parser = redditParser(url)
reddit_parser.get_current_user()
print(reddit_parser.users)