当前位置 博文首页 > 小航冲冲冲的博客:Python爬虫爬取纵横中文网小说

    小航冲冲冲的博客:Python爬虫爬取纵横中文网小说

    作者:[db:作者] 时间:2021-07-04 10:00

    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

    如有错误,敬请指正

    cs