Python爬虫爬取纵横中文网小说

Python爬虫爬取纵横中文网小说

学了一周的爬虫,搞了这个东西,自己感觉还不错,有什么问题可以提一提哈

目标:纵横中文网-完本-免费小说
网址:http://book.zongheng.com/store/c0/c0/b0/u0/p1/v0/s1/t0/u0/i1/ALL.html

如图:
在这里插入图片描述

我们的方向是:
爬取所有免费完本小说(实现翻页获取所有小说)——》进入小说具体页面——》进入小说目录——》进入小说具体章节——》获取标题以及文字

有了具体方向,我们开始实现代码

代码如下:

#纵横中文网-完本-免费 http://book.zongheng.com/store/c0/c0/b0/u0/p1/v0/s1/t0/u0/i1/ALL.html

import requests
import os
import time
from lxml import etree

# 获取每个页面的标签
for ml_url_http in range(1,16):
    ml_url_http = str(ml_url_http)
    #遍历获取每个页面
    yemian = 'http://book.zongheng.com/store/c0/c0/b0/u0/p' + ml_url_http + '/v0/s1/t0/u0/i1/ALL.html'
    #目录页面
    ml_url = yemian
    # UA伪装请求头
    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chromeh/81.0.4044.138 Safari/537.36'
    }
    response = requests.get(url=ml_url,headers=header).text
    treee = etree.HTML(response)
    ht = treee.xpath('//div[@class="bookname"]/a/@href')
    # 遍历所有书籍目录地址
    
    for htt in ht:
        res = requests.get(url=htt,headers=header).text
        htt_tree = etree.HTML(res)
        htt_tr = htt_tree.xpath('//a[@class="all-catalog"]/@href')
        #获取一本书籍的目录
        
        for hh in htt_tr:
            resp = requests.get(url=hh,headers=header).text
            tree = etree.HTML(resp)
            hh_tree = tree.xpath('//ul[@class="chapter-list clearfix"]/li[@class=" col-4"]')
            #提取标题
            hh_title = tree.xpath('//div[@class="book-meta"]/h1/text()')[0]
            # 创建文件
            try:
                os.makedirs('./纵横中文网爬取/' + hh_title)
            except Exception:
                print('文件已创建!')
                
            #获取目录超链接
            for hh_tree_li in hh_tree:
                #获取a标签里的href
                li = hh_tree_li.xpath('./a/@href')
                #获取每章节小说
                
                for http in li:
                    respon = requests.get(url=http,headers=header).text
                    http_tree = etree.HTML(respon)
                    #标题
                    http_title = http_tree.xpath('//div[@class="title_txtbox"]/text()')[0]
                    #创建txt文件
                    dizhi = './纵横中文网爬取/' + hh_title + '/' + http_title + '.txt'
                    fp = open(dizhi,'w+',encoding='utf-8')
                    #文字
                    http_t = http_tree.xpath('//div[@class="content"]/p')
                    
                    #遍历提取文字
                    for http_t_p in http_t:
                        #提取文字详细信息
                        p = http_t_p.xpath('./text()')[0]
                        #写入文件
                        fp.write(p)
                    print(http_title,'爬取成功!')
        print('\n' + '准备爬取下一本小说。。。' + '\n')
        #休眠两秒,以防爬取太快被网站反爬
        time.sleep(2)

爬取效果如下:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

以上代码仍有不足之处:在爬取到第五/六本小说时,会有验证码弹出,无法继续爬取

解决方案:
1.在验证码出现页面使用超级鹰验证码识别继续进行爬取
2.使用代理IP

如有错误,敬请指正

  • 4
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小航冲冲冲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值