-
Notifications
You must be signed in to change notification settings - Fork 0
/
spider.py
executable file
·147 lines (118 loc) · 4.06 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
#!/bin/python3
from gevent import monkey; monkey.patch_all()
import gevent
import requests
import bs4
import re
import sys
import dmenu
import os
from time import time
def get_soup(url):
r = requests.get(url=url, headers=headers)
return bs4.BeautifulSoup(r.content, "html5lib")
def img_download(name, img_url):
file = f'{dir}/{name}.jpg'
if os.path.exists(file):
print(f'Exists {file}')
else:
print(f'Downloading {file}')
r = requests.get(url=img_url, headers=headers)
gevent.sleep(0)
open(file, 'wb').write(r.content)
def save_url(page_dict):
import yaml
f = f'{dir}/page_dict.yaml'
if not os.path.exists(f):
os.mknod(f)
with open(f, 'r') as file:
try:
page_dict |= yaml.load(file)
except:
pass
with open(f, 'w') as file:
yaml.dump(page_dict, file, allow_unicode=True)
print(f'finish {f}')
class spider(object):
def __init__(self, url):
self.url = url
self.category = {}
self.soup_list =[]
self.page_dict = {}
soup = get_soup(self.url)
self.get_category(soup)
self.get_menu()
self.get_first()
self.get_pages()
def get_category(self, soup):
div = soup.select('.area')
a = div[0].find_all('a')
for i in a:
self.category |= {i.text: url + i.get('href') }
def get_menu(self):
select_category = dmenu.show(lines=15, items=self.category.keys())
self.select_category = self.category[select_category]
def get_first(self):
self.first_soup = get_soup(self.select_category)
div = self.first_soup.select('.panel')
text = div[-2].text.split('/')[-1]
count = text.split('页')[0]
self.count = dmenu.show('',prompt=count)
def get_gevent_soup(self, url):
r = requests.get(url=url, headers=headers)
gevent.sleep(0)
return bs4.BeautifulSoup(r.content, "html5lib")
# self.soup_list.append(bs4.BeautifulSoup(r.content, "html5lib"))
def get_content(self, soup):
div = soup.select('.thumbnail-group')
a = div[0].find_all('a')
page_dict = {}
for i in a:
text = re.sub('.html','play-1-1.html', i.get('href'))
page_dict |= {i.text: self.url + text}
img = div[0].find_all('img')
for i in img:
name = i.get('alt')
img_url = i.get('data-src')
img_download(name, img_url)
gevent.sleep(0)
return page_dict
# self.page_dict |= page_dict
def get_pages(self):
page_dict = {}
time_list = []
start = time()
page_dict |= self.get_content(self.first_soup)
end = time()
endtime='%.12f秒' % (end - start)
print(endtime)
time_list.append(endtime)
page_template = re.sub('.html','', self.select_category)
for i in range(2, int(self.count) + 1):
start = time()
page = f'{page_template}-{i}.html'
print(page)
soup = self.get_gevent_soup(page)
page_dict |= self.get_content(soup)
end = time()
endtime='%.12f秒' % (end - start)
print(endtime)
time_list.append(endtime)
# from time import time
# start = time()
# threads = [gevent.spawn(self.get_gevent_soup, f'{page_template}-{i}.html') for i in range(2, int(self.count) + 1)]
# gevent.joinall(threads)
# get_countent_threads = [gevent.spawn(self.get_content, i) for i in self.soup_list]
# gevent.joinall(get_countent_threads)
# end = time()
# print('%.12f秒' % (end - start))
print(time_list)
save_url(page_dict)
# save_url(self.page_dict)
if __name__ == "__main__":
dir = '/mnt/E/迅雷下载/.m/imgs'
if not os.path.exists(dir):
os.mkdir(dir)
url = sys.argv[1]
headers={'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64)' 'AppleWebKit/537.36 (KHTML, like Gecko)' 'Chrome/90.0.4430.85 Safari/537.36'}
spider(url)