-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscraper.py
140 lines (112 loc) · 3.8 KB
/
scraper.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# ______ ___
# / ____/___ ___ __ ______ ____ __________ _____/ (_)_______
# / __/ / __ `__ \/ / / / __ \/ __ `/ ___/ __ `/ __ / / ___/ _ \
# / /___/ / / / / / /_/ / /_/ / /_/ / / / /_/ / /_/ / (__ ) __/
#/_____/_/ /_/ /_/\__,_/ .___/\__,_/_/ \__,_/\__,_/_/____/\___/
# /_/
# _____
# / ___/______________ _____ ___ _____
# \__ \/ ___/ ___/ __ `/ __ \/ _ \/ ___/
# ___/ / /__/ / / /_/ / /_/ / __/ /
#/____/\___/_/ \__,_/ .___/\___/_/
# /_/
#
#!/usr/bin/python
from bs4 import BeautifulSoup
import sys, re, urllib2
def get_html(url):
"""Grab html code of a page given its URL"""
try:
usock = urllib2.urlopen(url)
html = usock.read()
usock.close()
return html
except:
return "error"
def get_html_cap(url):
"""Grab html code of a page given its URL"""
try:
opener = urllib2.build_opener()
opener.addheaders.append(('Cookie', 'downloadcaptcha=1'))
usock = opener.open(url)
html = usock.read()
usock.close()
return html
except:
return "error"
def make_soup(html_file):
soup = BeautifulSoup(html_file, "html.parser")
return soup
def get_name(i, table):
return soup.find('table')
class Console:
def __init__(self):
self.name = ''
self.link = ''
class ROM:
def __init__(self):
self.sys = ''
self.name = ''
self.link1 = ''
self.link2 = ''
def main():
dirs = {'gbc':'http://www.emuparadise.me/Nintendo_Game_Boy_Color_ROMs/11',\
'sega32x': "http://www.emuparadise.me/Sega_32X_ROMs/61",\
'genesis': "http://www.emuparadise.me/Sega_Genesis_-_Sega_Megadrive_ROMs/6",\
'segacd': "http://www.emuparadise.me/Sega_CD_ISOs/10",\
'atari2600': "http://www.emuparadise.me/Atari_2600_ROMs/49",\
'n64': "http://www.emuparadise.me/roms/n64/", \
'atari7800': "http://www.emuparadise.me/Atari_7800_ROMs/47",\
'gb': "http://www.emuparadise.me/Nintendo_Game_Boy_ROMs/12", \
'psp': "http://www.emuparadise.me/PSX_on_PSP_ISOs/67", \
'snes': "http://www.emuparadise.me/Super_Nintendo_Entertainment_System_(SNES)_ROMs/5", \
'atarilynx': "http://www.emuparadise.me/Atari_Lynx_ROMs/28",\
'gba': "http://www.emuparadise.me/Nintendo_Gameboy_Advance_ROMs/31", \
'nes': "http://www.emuparadise.me/Nintendo_Entertainment_System_ROMs/13", \
'psx': "http://www.emuparadise.me/roms/psx/"}
emu = "http://www.emuparadise.me"
gb_soup = BeautifulSoup(get_html("http://www.emuparadise.me/Nintendo_Game_Boy_ROMs/12"), "html.parser")
gb_table = gb_soup.find('table')
gb_list = []
sys_soup = []
game_list = []
i = 0
for k, v in dirs.iteritems():
soup = BeautifulSoup(get_html(v), "html.parser")
table = soup.find('table')
if table != None:
for x in table:
game_list.append(ROM())
game_list[i].sys = k
game_list[i].name = x.contents[0].contents[0].contents[0]
game_list[i].link1 = emu + x.contents[0].contents[0]['href'] + '-download'
i+=1
else:
list_div = soup.find(id="list-of-games")
for x in list_div.contents:
game_list.append(ROM())
game_list[i].sys = k
game_list[i].name = x.contents[0]
game_list[i].link1 = emu + x['href'] + '-download'
i+=1
print ("Done parsing " + k + " games")
i = 0
for x in game_list:
soup = BeautifulSoup(get_html_cap(x.link1), "html.parser")
link = soup.find_all(id="download-link")
if len(link) > 0:
x.link2 = emu + link[0]['href']
print ("Got link " + str(i))
i+=1
try:
out = open('out.txt', 'w')
except IOError:
print "cannot open file" "out"
#out.write("index\tsystem\ttitle\turl\n")
i = 1
for x in game_list:
out.write(str(i) + '\t' + x.sys + '\t' + x.name + '\t' + x.link2 + '\n')
print ("Wrote game number: " + str(i))
i+=1
out.close()
main()