-
Notifications
You must be signed in to change notification settings - Fork 0
/
spider.py
170 lines (99 loc) · 5.23 KB
/
spider.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
from header import *
class Spider:
# Class variables (shared among all instances)
domain_name = ''
base_url = ''
manga_list = []
chapter_list = []
contents = []
def __init__(self, domain_name, base_url, manga_data_list):
Spider.domain_name = domain_name
Spider.base_url = base_url
# crawl manga list on home page
Spider.add_urls_to_list(Spider.gather_urls(base_url, 'manga'), 'manga')
i_manga_list = 0
while len(Spider.manga_list) < MAX_MANGA:
Spider.add_urls_to_list(Spider.gather_urls(Spider.manga_list[i_manga_list]), 'manga')
i_manga_list += 1
Spider.crawl_manga_data_list(manga_data_list)
def crawl_manga_data_list(manga_data_list):
for i_manga_list in range(len(Spider.manga_list)):
manga_data = {}
manga = Spider.manga_list[i_manga_list]
print(i_manga_list, '---------', manga)
request = requests.get(manga)
soup = BeautifulSoup(request.text, 'html.parser')
description_class = soup.find('p', class_='description-update')
description_info = description_class.find_all('br')
manga_data['manga_id'] = i_manga_list + 1
manga_data['manga_name'] = clean_string(description_info[1].previous_sibling)
manga_data['thumbnail'] = soup.find('meta', {'property':'og:image'})['content']
manga_data['author'] = clean_string(description_info[3].previous_sibling)
s1 = soup.find('p', class_='manga-collapse').find('p')
s2 = soup.find('meta', {'name':'description'})['content']
manga_data['description'] = intersect(str(s1), str(s2))
manga_data['categories'] = [x['title'] for x in description_class.find_all('a', class_='CateName')]
manga_data['last_update'] = find_last_update(soup.find('div', class_='content mCustomScrollbar').find_all('a'))
Spider.chapter_list.clear()
Spider.add_urls_to_list(Spider.gather_urls(manga, 'chapter'), 'chapter', manga)
# insensitive sort
Spider.chapter_list = sorted(Spider.chapter_list, key = lambda s : s.lower())
#Spider.chapter_crawled.clear()
chapter_data_list = []
Spider.crawl_chapter_data_list(chapter_data_list)
manga_data['chapters'] = chapter_data_list
manga_data_list.append(manga_data)
def crawl_chapter_data_list(chapter_data_list):
for i_chapter_list in range(len(Spider.chapter_list)):
chapter_data = {}
chapter = Spider.chapter_list[i_chapter_list]
print(chapter)
Spider.contents.clear()
Spider.add_urls_to_list(Spider.gather_urls(chapter, 'content'), 'content')
request = requests.get(chapter)
soup = BeautifulSoup(request.text, 'html.parser')
chapter_data['chapter_id'] = i_chapter_list + 1
chapter_data['chapter_name'] = soup.find('h1', class_='chapter-title').text
chapter_data['page_list'] = Spider.contents
chapter_data_list.append(chapter_data)
def gather_urls(url, type = ''):
results = []
request = requests.get(url)
soup = BeautifulSoup(request.text, 'html.parser')
if type == 'content':
contents = soup.find('div', class_='each-page')
if contents == None:
contents = soup.find('div', class_='OtherText')
imgs = contents.find_all('img')
for img in imgs:
results.append(img['src'])
elif type == 'chapter':
chapters = soup.find('div', class_='content mCustomScrollbar').find_all('a')
for chapter in chapters:
results.append(chapter['href'])
elif type == 'manga':
for elem in soup.find_all('a', attrs={'href': re.compile('^http://')}):
url = urljoin(url, elem['href'])
results.append(url)
return results
def add_urls_to_list(urls, type, manga_url = ''):
for url in urls:
if (type == 'manga') and (len(Spider.manga_list) >= MAX_MANGA):
return
if (type != 'content') and (Spider.domain_name != get_domain_name(url)):
continue
if type == 'manga':
if (url in Spider.manga_list):
continue
if is_manga_url(url):
Spider.manga_list.append(url)
elif type == 'chapter':
if (url in Spider.chapter_list) or (not is_sub_url(url, manga_url)):
continue
if is_chapter_url(url):
Spider.chapter_list.append(url)
elif type == 'content':
if (url in Spider.contents):
continue
if is_content_url(url):
Spider.contents.append(url)