Python + Requests + Scrapy + Fcntl: 双引擎下载小说(简述)

本文介绍了使用Python的Requests、Scrapy和Fcntl库实现双进程引擎下载小说的方案。双引擎选择双进程而非多线程,以达到更好的资源控制和稳定性。文中涉及流程图、下载目录和章节的处理,以及UML时序图的展示,详细解释了如何利用这些库进行小说下载和同步控制。
摘要由CSDN通过智能技术生成

前言

双引擎

双引擎一般理解为双线程或双进程。这里指的是双进程的双引擎。双线程和双进程各有优略。BZ的理解是,线程消耗更少的系统资源,进程更容易控制。进程是系统基本的执行单元,线程依赖于进程不能单独存在。多线程的魅力在于一个进程可以包含多个线程。相对于一个进程启动同等数量的子进程来说,这明显占用更少的系统资源。但是线程也有一个很明显的缺点,就是比较难以控制。在强制关闭后,难以及时响应。进程在操作系统层面“可视化”程度较高。即使出现意外或基于临时的需求,双进程的双引擎包含多层面的多种终止运行的办法。基于稳妥的因素而并非极致追求效率的缘故,双进程引擎是不错的选择。

流程图

下载目录

Created with Raphaël 2.3.0 Start Enter URL or q Empty? Not 'q'? Valid URL? Download Get next URL Invalid URL? End End yes no yes no yes no yes no

下载章节

Created with Raphaël 2.3.0 Start Enter URL or q Empty? Cointinue? Valid URL? File existed? Lock the file Succeed? Download chapter Get next page Last page? End Create empty file Next page from category End yes no yes no yes no yes no yes no yes no

UML图

时序图

基本片段

Proc1 System Proc2 Is URL valid? Yes File existed? No Then create it, please? Create Fine, and it's done! I would like to lock it, ok? Lock No problem! Download & Write Can I have access to it? No, sorry! I know you have the file. So, just do it! I'm just doing my own job... I would get next chapter from local category file, since it's locked. Next chapter File existed? No Then create it! Ok Create I would like to lock it, ok? Lock Fine! Download & Write Job done! Release it, please! Unlock It's released! Is the end? Yes Quit Proc1 System Proc2

Requests

导入包含requests、自定义的httpkit.py模块。

from httpkit import *

Scrapy

bash命令行pip安装Scrapy。

~$: pip install scrapy

导入scrapy.Selector分析HTML源码。

from scrapy import Selector

Fcntl

fcntl是Python3内置库,是关于文件锁的模块。用来处理异步任务中协同工作的数据交换和资源调配问题。

import fcntl

代码

附录

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值