python简单源代码-python爬虫练习,一段简单的源码,求指点!

[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"

(.*?)script",chapter_html,re.S)[0]

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值