点击查看代码
# @Author: 昊阳
# _*_ coding : utf-8 _*_
# @Time:2022/10/21 0021 20:26
# @File : xiaoshuo
# @Project : MyCase
import parsel
import requests
from lxml import etree #这个是xpath解析
import re #这个是正则解析
# https://www.qb5.tw/book_991/ #完美世界
# https://www.qb5.tw/book_115322/ #大奉打更人
# https://www.qb5.tw/book_8227/ #斗破苍穹
# 正文
url = "https://www.qb5.tw/book_8227/"
headers = {
'User-Agent':' Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'
}
response = requests.get(url=url).text
list_url = re.findall('<dd><a href="(.*?)">(.*?)</a></dd>',response)
for book_url in list_url:
# print(book_url)
# 章节地址
urls = url + book_url[0]
# 章节名称
text = book_url[1]
name = re.sub('[,;;youle: :.;,$*#/ ]', "", text)
# print(urls)
# print(name)
data = requests.get(url=urls).text
content = etree.HTML(data)
bookcontent = content.xpath('//div[@id="content"]/text()')
# 字符串拼接
book_content = '\n'.join(bookcontent).replace(' ','').replace('全本小说网 www.qb5.tw,最快更新\n最新章节!','')
#下载小说
with open(r'斗破苍穹/'+name+'.txt',mode='a',encoding='utf-8')as f:
f.write(book_content)
print(name,'小说下载成功')
# print(response)