-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspyder.py
75 lines (64 loc) · 2.39 KB
/
spyder.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
from urllib.request import urlopen
from link_finder import LinkFinder
from general import *
class spider:
# class variables shared among all instances
project_name = ''
base_url = ''
domain_name = ''
queued_file = ''
crawled_file = ''
queue = set()
crawled = set()
def __init__(self, project_name, base_url, domain_name):
spider.project_name = project_name
spider.base_url = base_url
spider.domain_name = domain_name
spider.queued_file = spider.project_name+'/queue.txt'
spider.crawled_file = spider.project_name+'/crawled.txt'
self.boot()
self.crawl_page('spider 1', spider.base_url)
@staticmethod
def boot():
create_project_directory(spider.project_name)
create_data_files(spider.project_name, spider.base_url)
spider.queued = file_to_set(spider.queued_file)
spider.crawled = file_to_set(spider.crawled_file)
@staticmethod
def crawl_page(thread_name, page_url):
if page_url not in spider.crawled:
print(thread_name + ' now crawling ' + page_url)
print('Queued: ' + str(len(spider.queue)) + ' | Crawled: ' + str(len(spider.crawled)))
spider.add_links_to_queue(spider.gather_links(page_url))
spider.queue.remove(page_url)
spider.crawled.add(page_url)
spider.update_files()
@staticmethod
def gather_links(page_url):
html_string = ''
try:
response = urlopen(page_url)
if 'text/html' in response.getheader('Content-Type'):
html_bytes = response.read()
html_string = html_bytes.decode('utf-8')
finder = LinkFinder(spider.base_url, page_url)
finder.feed(html_string)
except Exception as e:
print('Error: cannot crawl page | check internet connection')
print(str(e))
return set()
return finder.page_links()
@staticmethod
def add_links_to_queue(links):
for url in links:
if url in spider.queue:
continue
if url in spider.crawled:
continue
if spider.domain_name not in url:
continue
spider.queue.add(url)
@staticmethod
def update_files():
set_to_file(spider.queue, spider.queued_file)
set_to_file(spider.crawled, spider.crawled_file)