免责声明:本文章涉及到的应用仅供学习交流使用,不得用于任何商业用途,数据来源于互联网公开内容,没有获取任何私有和有权限的信息(个人信息等)。由此引发的任何法律纠纷与本人无关!禁止将本文技术或者本文所关联的Github项目源码用于任何目的。
大部分小说网站都这样(特别是各种那个网站) 不懂得块可以百度, 代码仅供参考
目标网站 :https://www.xxxbiquge(.com) 2022/5/24 目前还在 。
需要的工具: python ;
第三方库 :parsel request.....(自己下载)
import requests
import re
import parsel
import os
第一步 :获取需要网页源码 拿到爬取小说的源码
# url = input("请输入需要爬取的小说:")
url = 'https://www.xxxbiquge.com/5/5900/'
headers = {
"User-Agent": # 用自己的 或者挂代理
}
resp = requests.get(url, headers=headers)
二 :拿到源码信息 每个章节的url(需要拼接) 标题(作为文件题目)
# 拿到 小说标题
select0 = parsel.Selector(resp.text) # 解析网页
novel_name = select0.xpath("/html/body/div[1]/div[5]/div[1]/div[2]/div[1]/div[1]/h1/text()").get()
# print(novel_name)
# re xpath scc 去找小说内容
# re拿到小说地址
url_child = re.findall('<a style="" href="(.*?)">(.*?)</a>', resp.text)
for novel in url_child:
# print(url)
novel_name_child = novel[1]
novel_url = "https://www.xxxbiquge.com" + novel[0]
三 :访问每个网页然后拿到小说内容
resp_child = requests.get(novel_url, headers=headers).text
# print(resp_child)
select = parsel.Selector(resp_child)
novel_title = select.xpath('/html/body/div[1]/div[5]/div/div/div[2]/h1/text()').get()
# 去除 标题写入的符号
novel_title = re.sub('[\\\/:?!<>]', '', novel_title)
novel_comtent_list = select.css('#content::text').getall()
novel_comtent = '\n'.join(novel_comtent_list)
# print(novel_comtent)
四:用os创建文件
novel_name = re.sub('[\\\/:?!<>]', '', novel_name)
if not os.path.exists(f"novel\\" + novel_name):
os.mkdir(f"novel\\" + novel_name)
最后保存: 记得关闭
with open(f'novel\\{novel_name}\\{novel_title}.txt', mode='a', encoding='utf-8') as f:
f.write(novel_title)
f.write("\n")
f.write(novel_comtent)
print("开始打印:" + novel_title)
print("打印完成")
resp.close()
全部代码:
import requests
import re
import parsel
import os
# url = input("请输入需要爬取的小说:")
url = 'https://www.xxxbiquge.com/5/5900/'
headers = {
"User-Agent":
}
resp = requests.get(url, headers=headers)
# print(resp.text)
# 拿个 小说标题
select0 = parsel.Selector(resp.text)
novel_name = select0.xpath("/html/body/div[1]/div[5]/div[1]/div[2]/div[1]/div[1]/h1/text()").get()
# print(novel_name)
novel_name = re.sub('[\\\/:?!<>]', '', novel_name)
if not os.path.exists(f"novel\\" + novel_name):
os.mkdir(f"novel\\" + novel_name)
# re xpath scc 去找小说内容
url_child = re.findall('<a style="" href="(.*?)">(.*?)</a>', resp.text)
for novel in url_child:
# print(url)
novel_name_child = novel[1]
novel_url = "https://www.xxxbiquge.com" + novel[0]
# print(novel_url, novel_name)
resp_child = requests.get(novel_url, headers=headers).text
# print(resp_child)
select = parsel.Selector(resp_child)
novel_title = select.xpath('/html/body/div[1]/div[5]/div/div/div[2]/h1/text()').get()
# 去除 标题写入的符号
novel_title = re.sub('[\\\/:?!<>]', '', novel_title)
novel_comtent_list = select.css('#content::text').getall()
novel_comtent = '\n'.join(novel_comtent_list)
# print(novel_comtent)
# 创建存储文件
with open(f'novel\\{novel_name}\\{novel_title}.txt', mode='a', encoding='utf-8') as f:
f.write(novel_title)
f.write("\n")
f.write(novel_comtent)
print("开始打印:" + novel_title)
print("打印完成")
resp.close()