-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguidaye.py
More file actions
135 lines (129 loc) · 4.04 KB
/
Copy pathguidaye.py
File metadata and controls
135 lines (129 loc) · 4.04 KB
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
import re
import sys
import json
import os.path
from itertools import count
from threading import Thread
import bs4
import requests
from bs4.element import Tag, ResultSet
def handle_link(link: dict, i: int, result: list):
text = link['title']
print('开始爬取', text)
content = requests.get('https://b.guidaye.com' + link['pic']).content.decode()
content = re.search(r'<div.+?id="nr1".+?>([\s\S]+?)</div>', content).group(1)
content = re.sub(r'src="/(.+?)"', 'src="https://b.guidaye.com/\\1"', content)
result[i] = (text, content)
print('爬取', text, '完成')
def main(id: str, title: str):
result = []
for n in count():
data = json.loads(requests.post(
'https://b.guidaye.com/e/extend/bookpage/pages.php?id={}'.format(id),
{'pageNum': n }
).content.decode())
if data['totalPage'] == n :
break
print('第', n + 1, '页')
former_length = len(result)
result.extend([None] * len(data['list']))
threads = []
for i, link in enumerate(data['list'], former_length):
thread = Thread(target=handle_link, args=(
link,
i,
result
))
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
write_file(title, result)
def write_file(title: str, result: list):
_path = path
if path is None:
_path = input('输入路径 ')
if not _path:
_path = title + '.html'
open(_path, 'w', encoding='utf-8').write(
"""<html>
<head>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1 class="text-primary" id="top">{}</h1>
<div class="row">
<div class="col-4">
<label>改变字体</label>
</div>
<div class="col">
<input type="range" class="form-range" id="size" min="10" max="50">
</div>
</div>
<div class="row">
<div class="col-3">
<nav class="navbar navbar-light bg-light flex-column align-items-stretch p-3">
{}
</nav>
</div>
<div class="col" id="content">
{}
</div>
</div>
<div class="fixed-bottom">
<a href="#top">回到顶部</a><br>
<a href="http://172.31.2.4:19198">回到主站</a>
</div>
</div>
<script>
const size = document.getElementById('size');
const content = document.getElementById('content');
size.addEventListener('change', (e) => {{
content.style = 'font-size: ' + Math.floor(size.value);
}});
</script>
</body>
</html>""".format(
title,
'\n'.join(
'<nav class="nav nav-pills flex-column"><a class="nav-link" href="#_{}">{}</a></nav>'.format(
i, text
)
for i, (text, _) in enumerate(result)
),
'\n'.join(
'<h4 id="_{}">{}</h4>\n{}'.format(
i, text, content
)
for i, (text, content) in enumerate(result)
)
)
)
print('写入到', os.path.abspath(_path), '.')
def search(title: str) -> ResultSet:
soup = bs4.BeautifulSoup(requests.post('https://b.guidaye.com/e/search/index.php', {
'keyboard': title,
'show': 'title',
'tempid': 1
}).content.decode(), 'lxml')
return soup.find('ul', class_='search-novel-list').find_all('a')
if __name__ == '__main__':
_, title, *path = sys.argv
if path:
path = path[0]
else:
path = None
options = search(title)
if not options:
print('未找到')
else:
prompt = '选择书籍:\n{}\n'.format(
'\n'.join(
'{} {} {}'.format(i, book.get_text(), book['href'])
for i, book in enumerate(options)
)
)
selection = options[int(input(prompt))]
id = re.match('.+/(\d+)/?$', selection['href']).group(1)
main(id, selection.get_text())