-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscrape.py
112 lines (86 loc) · 2.68 KB
/
scrape.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
import re
import os
import urllib2
import urlparse
from optparse import OptionParser
def main():
parser = OptionParser(usage = "usage = %prog OPTIONS domain url")
parser.add_option("--out", dest = "out")
(options, args) = parser.parse_args()
if len(args) < 2:
print "Incorrect arguments"
parser.print_help()
exit(1)
directory = options.out or os.getcwd()
first = args[1]
domain = args[0]
processed = []
queue = [ first ]
count = 0
if not os.path.isdir(directory):
os.makedirs(directory)
if not domain in queue[0]:
print "Provided domain not found in start URL"
parser.print_help()
exit(1)
print "saving in {}".format(directory)
print "using domain {}".format(domain)
print "start URL {}".format(queue[0])
while len(queue) > 0:
url = queue.pop()
if url in processed:
continue
parsed = urlparse.urlparse(url)
if domain not in parsed.netloc and not is_relative_url(url):
processed.append(url)
continue
print "scraping {}".format(url)
(urls, data, is_html) = scrape_next(url, first)
if data is None:
processed.append(url)
continue
if is_html:
queue.extend([s for s in urls if (domain in s or is_relative_url(s) and \
s not in queue and s not in processed)])
write_local_data(directory, parsed, data, is_html)
processed.append(url)
count += 1
print "done -- processed {} urls".format(count)
exit(0)
def is_relative_url(url):
return re.match(r'^\w+://', url) is None
def write_local_data(directory, parsed_url, data, is_html):
path = os.path.abspath(directory + parsed_url.path + parsed_url.query)
if not path.endswith('.html') and is_html:
if not path.endswith(os.sep):
path += os.sep
path += 'index.html'
directory = os.path.dirname(path)
if not directory.endswith(os.sep):
directory += os.sep
if not os.path.isdir(directory):
os.makedirs(directory)
with open(path, 'wb') as f:
f.write(data)
def scrape_next(url, url_base):
urls = []
url = urlparse.urljoin(url_base, url)
try:
response = urllib2.urlopen(url)
except urllib2.HTTPError as e:
print "http error {} reading {}: {}".format(e.code, url, e.reason)
return (None, None, None)
except urllib2.URLError as e:
print "bad url {}: {}".format(url, e.reason)
return (None, None, None)
except:
print "unexpected error reading {}".format(url)
return (None, None, None)
data = response.read()
is_html = response.info().getsubtype() == "html"
response.close()
if is_html:
urls = re.findall(r'(?:href|src)=[\'"]?([^\'" >]+)', data)
return (urls, data, is_html)
if __name__ == "__main__":
main()