[Asm] 纯文本查看 复制代码import re
import requests
from lxml import etree
class LingdianKanshu:
def __init__(self):
self.session = requests.Session()
def run(self, url):
"""下载小说"""
index_html = self.download(url)
# 小说的标题
title = etree.HTML(index_html)
title = title.xpath("string(//h1)")
# 提取章节信息,url网址
chapter_infos = self.get_chapter_info(index_html)
# 创建一个文件 小说名.txt
fb = open("%s.txt" % title, "w",encoding="utf-8")
# 下载章节信息 循环
for chapter_info in chapter_infos:
chapter_info = chapter_info.xpath("@href|text()")
link = f"{start_url+chapter_info[0]}"
fb.write("%s " % chapter_info[1])
# 下载章节
content = self.get_chapter_content(link)
fb.write(content)
fb.write(" ")
print(chapter_info)
fb.close()
def download(self, url):
"""下载html源码"""
response = self.session.get(url)
response.encoding = response.apparent_encoding
html = response.text
return html
def get_chapter_info(self, index_html):
tree = etree.HTML(index_html)
d1 = tree.xpath("//dd/a")
return d1
def get_chapter_content(self, link):
"""下载章节内容"""
chapter_html = self.download(link)
content = re.findall(
r"
content=re.sub("w.*?m","",content)
content = re.sub("W.*?M", "", content)
# 清洗 数据
content = content.replace(" ", "")
content = content.replace("
", "")
content = content.replace("", "")
content = content.replace(r"\", "")
content = content.replace("//", "")
content = content.replace("<", "")
# 替换换行
# content = " ".join(content.split())
content = content.replace(
"Ps:书友们,我是烟雨江南,推荐一款免费小说App,支持小说下载、听书、零广告、"
"多种阅读模式。请您关注微信公众号:dazhuzaiyuedu(长按三秒复制)书友们快关注起来吧!", "")
return content
if __name__ == "__main__":
start_url = "https://www.lingdiankanshu.co/16817/"
kanshu = LingdianKanshu()
kanshu.run(start_url)