码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Python爬虫实战-小说网站爬虫开发


    需求:

    从http://www.kanunu8.com/book3/6879爬取《动物农场》所有章节的网址,再通过一个多线程爬虫将每一章的内容爬取下来。在本地创建一个“动物农场”文件夹,并将小说中的每一章分别保存到这个文件夹中。每一章保存为一个文件。
    在这里插入图片描述
    涉及到的知识点
    1、requests爬虫网站内容
    2、正则表达式提取内容
    3、文件写入
    4、多线程

    插话:做这类需求,最好还是先自己想,自己实现,实现后再去看自己跟书上的有什么不一样。

    单线程实现

    #使用requests获取网页源代码
    import requests
    import re
    import time
    
    #获取首页源码
    html=requests.get('https://www.kanunu8.com/book3/6879/').content.decode(encoding='gbk')
    # print(html)
    #获取所有章节链接
    herf=re.findall('',html,re.S)
    print(herf)
    start=time.time()
    for i in herf:
        #通过链接获取每一章的源码
        chapter_html=requests.get('https://www.kanunu8.com/book3/6879/'+i).content.decode(encoding='gbk')
        # print(chapter_html)
        title=re.search('size="4">(.*?)<',chapter_html,re.S).group(1)#获取章节名称
        content=re.findall('

    (.*?)

    ',chapter_html,re.S)#获取每一张p标签内的内容,结果返回为列表 content_str="\n".join(content).replace("
    ","")#列表转为字符串并替换多余符号 with open('动物农场/'+title+'.txt','w',encoding='utf-8') as f: f.write(title) f.write(content_str) end=time.time() print(f'单线程耗时{end-start}')
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    结果,文件夹是自己手动创建的,生成如下十个章节txt文件。
    文件为每个章节的内容。
    如果看不到,在文件夹右键,reload from disk
    在这里插入图片描述
    这个记录了下耗时,可以看到是13秒多。
    单线程耗时13.868733167648315

    多线程

    #使用requests获取网页源代码
    import requests
    import re
    import time
    from multiprocessing.dummy import Pool
    def get_html():
        #获取首页源码
        html=requests.get('https://www.kanunu8.com/book3/6879/').content.decode(encoding='gbk')
        # print(html)
        #获取所有章节链接
        herf=re.findall('',html,re.S)
        return herf
    
    def get_every_chapter(i):
            #通过链接获取每一章的源码
            chapter_html=requests.get('https://www.kanunu8.com/book3/6879/'+i).content.decode(encoding='gbk')
            # print(chapter_html)
            title=re.search('size="4">(.*?)<',chapter_html,re.S).group(1)#获取章节名称
            content=re.findall('

    (.*?)

    ',chapter_html,re.S)#获取每一张p标签内的内容,结果返回为列表 content_str="\n".join(content).replace("
    ","")#列表转为字符串并替换多余符号 with open('动物农场/'+title+'.txt','w',encoding='utf-8') as f: f.write(title) f.write(content_str) if __name__ == '__main__': start = time.time() herf=get_html()#html内容 print(herf) pool = Pool(3)#创建三个线程 pool.map(get_every_chapter,herf) #章节链接列表传给函数入参 end=time.time() print(f'多线程耗时{end-start}')
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    这里使用了3个线程,耗时5.348664283752441
    需要注意的一个点,map的第一个参数只写函数名,不用写()
    4个线程,耗时4.111229181289673
    另外试了5个线程,耗时耗时5.717579126358032
    可见,3,4,5中,4个线程耗时最少,线程数不是越多越好。

    书上实现

    创建文件夹

    文件路径
    在这里插入图片描述
    且实现了一个保存文件的函数
    在这里插入图片描述
    爬虫部分差不多,需要注意的是findall输出为列表,和search结果为字符串。
    在这里插入图片描述
    总结:先自己写,再看书上实现,才能锻炼能力。
    内容参考:
    在这里插入图片描述

  • 相关阅读:
    PicoRV32-on-PYNQ-Z2: An FPGA-based SoC System——RISC-V On PYNQ项目复现
    使用 Hibernate Envers 进行实体审计
    基于 webpack 5 实现自定义 loader
    What is SVM algorithm
    JDBC和DBUtils框架的使用
    【物联网】Arduino+ESP8266物联网开发(一):开发环境搭建 安装Arduino和驱动
    MyBatis-Plus(三、增删改查)
    defer-promise 源码解析(Npm library)
    嵌入式开发:为什么无触摸手势对嵌入式GUI开发团队至关重要
    【编程英语】Python常用英语单词
  • 原文地址:https://blog.csdn.net/seanyang_/article/details/126315569
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号