[Python] 纯文本查看 复制代码import re
import urllib.request
import os
import time
from urllib.parse import quote
search_input = input("请输入搜索的书名或作者:")
search_Book = "https://www.qidian.com/search?kw=" + quote(search_input)
page = urllib.request.urlopen(search_Book).read()
txt = page.decode('utf-8')
def bookname():
filter_bookname = r'
(.+?)
'book_name_nosub = re.findall(filter_bookname, txt, re.S)
book_name_sub = re.sub(r'||', '', " ".join(book_name_nosub))
book_name = book_name_sub.split(' ') # 书名
return book_name
def bookChapter():
filter_Chapter = r'
number=["⓪","⓵","⓶","⓷","⓸","⓹","⓺","⓻","⓼","⓽"]
'bookname = re.findall(filter_bookname, page, re.S)
(.+?)
' #第4章 继任者
chaptername = re.findall(filter_chaptername, page, re.S) # 获取章节和章节名字
i = 0
for txt in html:
line = html[i]
f = open(img_path + chaptername[0] + ".txt", "a") # a代表追加模式,不覆盖
f.write(line + "\n")
f.close()
i = i + 1
print(chaptername[0] + " 下载完成")
next = r'下一章
nextread = re.findall(next, page, re.S)
b = ''
link = "https://" + b.join(nextread) # 本页的下一章链接
z = z + 1
img_path2 = "E:/txt/" + bookname[0]
c = 0
if not os.path.exists(img_path2): # 如果文件不存在,则重命名文件
os.rename(img_path, img_path2) # 文件夹重命名
print("下载完成")
else:
path = "E:/txt/txt/"
downloadtime = time.strftime("%Y%m%d%I%M%S", time.localtime())
os.rename(img_path, img_path2 + downloadtime)
print("文件名:" + bookname[0] + " 已存在,重命名为:" + bookname[0] + downloadtime + "\n" + "请勿重复操作")