起点小说网小说爬取

该博客主要介绍如何从起点小说网获取小说目录和章节网址。通过搜索小说,利用开发者工具找到JSON数据源,解析获取章节信息,并构建小说章节的网址。虽然VIP资源后期被封锁,但提供了继续下载的可能性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

小说目录和网址的获取

进入官网,点击输入书名,点击搜索,进入免费试读。
打开开发者工具,刷新页面,找到小说目录json格式对应的网址
在这里插入图片描述
先获取json格式的数据

url = 'https://read.qidian.com/ajax/book/category?_csrfToken=pDSSk0OUANaaYIOXJfzVTB27IXwqdBF2qx0MEBqe&bookId=1924072'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0', }
response = requests.get(url=url, headers=headers)
# 用原编码格式,防止乱码
response.encoding = response.apparent_encoding
novel_json = json.loads(response.text)

然后获取json中小说对应的章节以及内容所对应的部分网址并构建小说的网址并保存到本地

# 此书一共10卷,若没有VIP限制则可全部下载,获取每一卷的章节及部分网址
for i in range(1, 11):
    chapters_info = novel_json['data']['vs'][i]['cs']
	for item in chapters_info:
	    ids = item['cU']
	    name = item['cN']
		url = 'https://read.qidian.com/chapter/'
	    url = url + ids
	    response = requests.get(url=url, headers=headers)
	    response.encoding = response.apparent_encoding
	     html = etree.HTML(response.text)
        contents = html.xpath('//*[@class="text-wrap"]/div/div[2]/p/text()')
        length = len(contents)
        content = ''
        for p in range(length):
            content = content + contents[p]
        content.replace('\u3000\u3000', '')
        print(content)
        chapter = name + '\n' + content + '\n'
        with open('独步天下.txt', 'a') as f:
            f.write(chapter)

效果图如下
在这里插入图片描述
下载前两卷后,vip资源已被封锁,不过可以从小网站下载。

以下是使用Python编写的爬取起点小说网的多线程爬虫代码: ```python import requests from lxml import etree from queue import Queue import threading # 定义爬虫类 class Spider(): def __init__(self, url, headers): self.url = url self.headers = headers self.session = requests.Session() self.session.headers.update(headers) # 获取小说列表 def get_novel_list(self): response = self.session.get(self.url) html = etree.HTML(response.text) novel_list = html.xpath('//div[@class="book-mid-info"]/h4/a/@href') return novel_list # 获取小说信息 def get_novel_info(self, novel_url): response = self.session.get(novel_url) html = etree.HTML(response.text) novel_info = {} novel_info['title'] = html.xpath('//div[@class="book-info "]/div[@class="book-info "]/h1/em/text()')[0] novel_info['author'] = html.xpath('//div[@class="book-info "]/div[@class="book-info "]/h1/span/a/text()')[0] novel_info['intro'] = html.xpath('//div[@class="book-intro"]/p/text()')[0] novel_info['word_count'] = html.xpath('//div[@class="book-info "]/div[@class="book-info "]/p/span[1]/text()')[0] return novel_info # 定义爬取线程类 class SpiderThread(threading.Thread): def __init__(self, spider, novel_queue): threading.Thread.__init__(self) self.spider = spider self.novel_queue = novel_queue def run(self): while True: try: novel_url = self.novel_queue.get(False) novel_info = self.spider.get_novel_info(novel_url) print(novel_info) except: break # 定义主函数 def main(): url = 'https://www.qidian.com/all' headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} spider = Spider(url, headers) novel_list = spider.get_novel_list() # 创建小说队列 novel_queue = Queue() # 将小说列表加入队列 for novel_url in novel_list: novel_queue.put(novel_url) # 创建爬取线程 threads = [] for i in range(5): spider_thread = SpiderThread(spider, novel_queue) spider_thread.start() threads.append(spider_thread) # 等待所有线程结束 for t in threads: t.join() if __name__ == '__main__': main() ``` 该代码使用了Python的requests库lxml库来进行网页爬取解析,使用了多线程来提高爬取效率。首先定义了一个Spider类来实现爬取小说列表小说信息的功能,然后定义了一个SpiderThread类来作为爬取线程,最后在主函数中创建小说队列爬取线程,并等待所有线程结束。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值