zoukankan      html  css  js  c++  java
  • 练习_Python3 爬取笔趣阁最新小说章节

    警告:本文代码仅供学习,禁止违法使用或商用。

    这里拿人气小说《黎明之剑》来举个栗子,喜欢小说《黎明之剑》的朋友们请支持正版阅读。

    笔趣阁网站上的其他书籍基本上的都可以套用,其他盗版网站也基本上是差不多的思路就可以解决。

    稍微改改就能很轻松的通过小说目录页下载全本,我这里就懒得弄了,有兴趣的朋友可以试一试。

    # -*- coding:UTF-8 -*-
    # 作者博客:https://www.cnblogs.com/Raine/
    # 2019-06-20
    
    import requests
    from bs4 import BeautifulSoup
    
    
    class TheLatest(object):
        # 测试爬取笔趣阁《黎明之剑》最新章节
        def __init__(self):
            self.url_dir = 'https://www.biqiuge.com/book/36438/'
            self.bookname = ""  # 存放书籍名
            self.chaptername = ""  # 存放章节名
            self.url_latest = ""  # 存放最新章节链接
            self.get_download_url()
    
        def get_download_url(self):
            # 直接从网页head标签内获取想要的内容
            r1 = requests.get(self.url_dir)
            # 网页是GBK编码,需要转换
            r1.encoding = 'GBK'
            html_1 = r1.text
            bs_div = BeautifulSoup(html_1, 'lxml')
            # 找到需要用到的标签然后提取属性
            _bookname = bs_div.find('meta', property="og:novel:book_name")
            self.bookname = _bookname.get('content')
            _chaptername = bs_div.find('meta', property='og:novel:latest_chapter_name')
            self.chaptername = _chaptername.get('content')
            _url_latest = bs_div.find('meta', property='og:novel:latest_chapter_url')
            self.url_latest = _url_latest.get('content')
    
        def get_content(self):
            r2 = requests.get(self.url_latest)
            r2.encoding = 'GBK'
            html_content = r2.text
            bs_div = BeautifulSoup(html_content, 'lxml')
            txt = bs_div.find('div', 'showtxt')
            # 优化文字排版
            txt = txt.text.replace('  ', '
      ')
            txt = txt.replace('�6�1', '·')
            out_content = txt.split(self.url_latest)[0]
            return out_content
    
    
    if __name__ == '__main__':
        txt_content = TheLatest()
        filename = txt_content.bookname + txt_content.chaptername + '.txt'
        with open(filename, 'w', encoding='utf-8') as f:
            f.write(txt_content.get_content())
    

      

    参考资料:

    Python3网络爬虫快速入门实战解析 :https://cuijiahua.com/blog/2017/10/spider_tutorial_1.html

    Python——爬虫【Requests设置请求头Headers】:https://blog.csdn.net/ysblogs/article/details/88530124

    Python3.x爬虫教程:爬网页、爬图片、自动登录 :https://blog.csdn.net/Evankaka/article/details/46849095

    while True :

        learning ()

  • 相关阅读:
    松软科技web课堂:字符串方法和属性
    松软科技web课堂:JavaScript 事件
    松软科技前端课堂:JavaScript 对象
    松软科技web课堂:JavaScript 数据类型
    MVN TEST指定运行脚本
    Cucumber语法格式
    Maven pom文件标签解析大全
    测试用例自动转换成自动化脚本
    Cucumber使用中问题
    Cucumber常用关键字
  • 原文地址:https://www.cnblogs.com/Raine/p/11060118.html
Copyright © 2011-2022 走看看