爬取17k小说网的小说

最近在学习python爬虫,所以写了一个17K小说网爬取的脚本来做练习,分享一下

1.爬取的网页为http://all.17k.com/lib/book.html 小说分类页面的免费区的小说,付费vip的没有账号

这事小说列表页​​​​在这里插入图片描述

这是小说说明页在这里插入图片描述

这是小说章节详情页在这里插入图片描述

这事小说的内容页在这里插入图片描述

因为小说网页为静态的页面,作为爬虫练习入门非常的简单了。我们倒着推先找到小说内容页的内容,通过xpath很简单就能获取到,
在这里插入图片描述
再根据小说章节页获取所有的章节的网址url,循环遍历,然后存储就把一本小说存储下来了,;
在这里插入图片描述
接下来我们只要获取所有小说的url列表就以此遍历就把小说存储下来了。
在这里插入图片描述
如下图,小说会以名字+作者的方式存储,内容会按照章节+文章保存,并且会赋存一分json文件,记录爬取的书的列表信息;
在这里插入图片描述

不太爱写文章所以 分享源码连接:https://download.csdn.net/download/weixin_39179620/10928691
部分代码(如下)

# coding=utf-8
'''
作者:Jguobao
QQ:779188083
email:jgb2010start@163.com
'''
import requests
from lxml import etree
import json


class NovelSpider:
    def __init__(self, start_url="http://all.17k.com/lib/book/2_0_0_0_0_0_1_0_1.html?"):
        self.start_url = start_url
        self.url = "http://www.17k.com/list/1038316.html"  # "http://www.17k.com/chapter/2938105/36788407.html"
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"}

    def parse_url(self, url):
        response = requests.get(url, headers=self.headers)
        if response.status_code != 200:
            return None
        return response.content.decode()

    def get_detail_page(self, html_str, index=""):
        if index != "":
            index = str(index) + "."
        html_str_etree = etree.HTML(html_str)
        title = html_str_etree.xpath("//div[@class='readAreaBox content']/h1/text()")[0]
        word = html_str_etree.xpath("//div[@class='readAreaBox content']/div[@class='p']/text()")
        word = ["  " + w.strip() + "\n" if len(w.strip()) > 0 else None for w in word]
        title = "\t\t\t" + index + title.strip() + "\n"
        for w in word[:]:
            if w is None:
                word.remove(w)
        return title, word[:-1]

此段为提取html信息的主要函数

    def get_txt_name(self, html_str):
        html_str_etree = etree.HTML(html_str)
        filename = html_str_etree.xpath("//div[@class='Main List']//h1/text()")[0] + '.txt' if len(
            html_str_etree.xpath("//div[@class='Main List']//h1")) > 0 else "无名"
        return filename

    def get_url_list(self, html_str):
        item_list = []
        html_str_etree = etree.HTML(html_str)
        url_list = html_str_etree.xpath("//dl[@class='Volume']//dd/a/@href")
        volume_list = html_str_etree.xpath("//dl[@class='Volume']")  # 获取所有的卷 并且从卷下获取所有的卷标 章节
        for volume in volume_list:
            item = {}
            item['卷标'] = volume.xpath(".//span[@class='tit']/text()")[0]
            item['info'] = volume.xpath(".//span[@class='info']/text()")[0]
            # 获取下面的所有的a标签
            a_list_etree = volume.xpath("./dd/a")
            a_list = []
            for a in a_list_etree:
                item2 = {}
                title2 = a.xpath(".//span[@class='ellipsis']/text()")[0].strip()
                item2[title2] = "http://www.17k.com" + a.xpath("./@href")[0]
                a_list.append(item2)
            item['章节'] = a_list

            item_list.append(item)
            # 根据a标签列表获取所有章节与对应的链接
        url_list = ["http://www.17k.com" + url for url in url_list]

        return item_list

此段为保存与处理一部小说的主要函数

    def save_txt(self, txt_list, filename):
        with open('./txt/' + filename, "a", encoding='utf8') as f:
            f.write(txt_list[0])
            for wd in txt_list[1][:-2]:
                f.write(wd)

    def process_item_list(self, item_list, filename):  # 获取小说的章节内容并保存
        for item in item_list:
            volume_label = item['卷标']
            volume_info = item['info']
            item_list = item['章节']
            lens2 = len(item_list)
            for item in item_list:
                for i in item:
                    print(i)
                    title = i
                    html_detail_str = self.parse_url(item[i])
                    txt_list = list(self.get_detail_page(html_detail_str))
                    txt_list[0] = '\t\t' + title + '\n'
                    self.save_txt(txt_list, filename)

创建对象,指定要爬取的页面以及要爬取的页数

if __name__ == '__main__':
 	#start_url默认值为:"http://all.17k.com/lib/book/2_0_0_0_0_0_1_0_1.html?" 也可以自己输入
    ns = NovelSpider()  # 输入start_url要爬取的网页 
    ns.run(1000) #run方法输入要爬取多少页
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用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类来作为线程,最后在主函数中创建小说队列和线程,并等待所有线程结束。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值