-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathui2.py
241 lines (213 loc) · 7.11 KB
/
ui2.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import os
import re
import tkinter as tk
from tkinter import ttk,messagebox
import parsel
import requests
import concurrent.futures
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def get_response_s(html_url):
"""
获取网页的响应内容
:param html_url: 网页链接
:return: 网页的完整页面源码
"""
# 使用 Chrome 浏览器
driver = webdriver.Edge()
driver.get(html_url)
try:
# 使用 WebDriverWait 等待动态加载的内容出现
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "bookinfo"))
)
# 获取包含动态加载内容的完整页面源码
html_source = driver.page_source
return html_source
finally:
# 关闭浏览器
driver.quit()
def get_response(html_url):
"""
发送请求函数
:param html_url: 请求链接
:return:response 响应对象
"""
# 模拟浏览器headers请求头
headers = {
# 用户代理 表示浏览器基本身份信息
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0'
}
# 发送请求
response = requests.get(url=html_url, headers=headers)
return response
def get_list(html_url):
"""
获取各章节url/小说名
:param html_url:小说目录页url
:return:小说名,章节url
"""
# 发送请求
html_data = get_response(html_url).text
# 提取小说名字
name = re.findall('<span class="title">(.*?)</span>', html_data)[0]
# 提取章节url
url_list = re.findall('<dd><a href ="(.*?)">', html_data)
return name, url_list
def get_content(html_url):
"""
获取小说内容/小说每章标题
:param html_url:小说章节url
:return:每章标题,内容
"""
response = get_response(html_url)
# 将获取的html字符串数据转成可解析对象
selector = parsel.Selector(response.text)
# 提取标题
title = selector.css('.reader h1::text').get()
# print(title)
# 提取内容
content = '\n'.join(selector.css('#chaptercontent::text').getall())
return title, content
def search(word):
"""
搜索小说/作者
:param word: 小说名/作者名
:return: 包含搜索结果的列表
"""
url = f'https://www.biqg.cc/s?q={word}'
search_data = get_response_s(url)
selector = parsel.Selector(search_data)
divs = selector.css('.type_show .bookinfo')
pre = '作者:'
print(len(divs))
data = []
for div in divs:
name = div.css('.bookname a::text').get()
href = div.css('.bookname a::attr(href)').get()[len('/book/'):-1]
author = div.css('.author::text').get()[len(pre):]
d = {
'href': href,
'name': name,
'author': author
}
data.append(d)
global book_data
book_data = data
return data
book_data = []
def show():
"""
显示查询结果
:return: None
"""
name = name_va.get()
print(f"查询{name}")
# data = [{'num': 524, 'author': 'anson', 'name': 'i am the best', 'href': 1},
# {'num': 563, 'author': 'mike', 'name': 'the way', 'href': 13}
# ]
data = search(name)
for i, v in enumerate(data):
tree_view.insert('', i + 1, values=(i + 1, v['author'], v['name'], v['href']))
def save(name, title, content):
"""
保存数据函数
:param name: 小说名
:param title: 章节名
:param content: 内容
:return: None
"""
# 创建文件
file = f'{name}\\'
if not os.path.exists(file):
os.mkdir(file)
with open(file + title + '.txt', mode='a', encoding='utf-8') as f:
"""
第一章 标题
内容
"""
f.write(title)
f.write('\n')
f.write(content)
f.write('\n')
def download_sub(sub, book_title):
"""
下载子页面内容并保存
:param sub: 子页面链接
:param book_title: 书籍标题
:return: None
"""
url = f"https://www.biqg.cc/{sub}"
title, content = get_content(url)
save(name=book_title, title=title, content=content)
print(title)
def download():
"""
下载书籍
:return: None
"""
url = 'https://www.biqg.cc/book/'
num = int(num_va.get()) - 1
global book_data
href = book_data[num]['href']
book_title, url_list = get_list(url + href)
print(f'开始下载:《{book_title}》')
print("===================")
with concurrent.futures.ThreadPoolExecutor(max_workers=1000) as executor:
futures = [executor.submit(download_sub, sub, book_title) for sub in url_list]
for future in concurrent.futures.as_completed(futures):
try:
future.result()
except Exception as e:
print(f"An error occurred: {e}")
print("===================")
print(f'《{book_title}》下载完成')
messagebox.showinfo("下载完成", f"《{book_title}》下载完成")
# 创建界面
root = tk.Tk()
# 变量
name_va = tk.StringVar()
num_va = tk.StringVar()
# 设置界面大小
root.geometry('500x500+500+200')
# 设置标题
root.title('小说下载器')
# 名字作者输入框
search_frame = tk.Frame(root)
search_frame.pack(pady=20) # 设置上边距
tk.Label(search_frame, text='书名 作者', font=('宋体', 15)).pack(side=tk.LEFT, padx=10)
tk.Entry(search_frame, textvariable=name_va).pack(side=tk.LEFT)
# 序号输入框
download_frame = tk.Frame(root)
download_frame.pack(pady=10) # 设置上边距
tk.Label(download_frame, text='序号', font=('宋体', 15)).pack(side=tk.LEFT, padx=33)
tk.Entry(download_frame, textvariable=num_va).pack(side=tk.LEFT)
# 查询下载
button_frame = tk.Frame(root)
button_frame.pack(pady=10)
tk.Button(button_frame, text='查询', font=(12), relief='flat', bg='#FAEBD7', width=8, command=show).pack(side=tk.LEFT,
padx=10)
tk.Button(button_frame, text='下载', font=(12), relief='flat', bg='#FAEBD7', width=8, command=download).pack(
side=tk.LEFT, padx=10)
# 设置表格
columns = ('num', 'writer', 'name', 'novel_id')
columns_value = ('序号', '作者', '书名', '书ID')
tree_view = ttk.Treeview(root, height=18, show='headings', columns=columns)
# 设置列名
tree_view.column('num', width=1, anchor='center')
tree_view.column('writer', width=30, anchor='center')
tree_view.column('name', width=90, anchor='center')
tree_view.column('novel_id', width=5, anchor='center')
# 给列名设置显示的名字
tree_view.heading('num', text='序号')
tree_view.heading('writer', text='作者')
tree_view.heading('name', text='书名')
tree_view.heading('novel_id', text='书ID')
tree_view.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
# 滚动条
vsb = ttk.Scrollbar(root, orient="vertical", command=tree_view.yview)
vsb.pack(side=tk.RIGHT, fill=tk.Y)
tree_view.configure(yscrollcommand=vsb.set)
root.mainloop()