Simpyder - Simple Python Spider
Simpyder - 轻量级协程Python爬虫
特点
- 轻量级:下载便利,依赖较少,使用简单。
- 协程:单线程,通过协程实现并发。
- 可定制:简单配置,适应各种爬取场合。
快速开始
下载
#使用pip3
pip3 install simpyder --user
# 更新包
pip3 install simpyder --upgrade
编码
用户只需要定义三个函数,实现三个模块:
链接获取
我们需要一个定义一个异步生成器,用于产生链接。
async def gen_url():
for each_id in range(100):
yield "https://www.biliob.com/api/video/{}".format(each_id)
链接解析
我们需要定义一个解析链接的函数。其中第一个参数是Response对象,也就是上述函数对应URL的访问结果。
该函数需要返回一个对象,作为处理结果。
注意,与普通函数不同,这是一个协程函数。需要在前面加上async。代表该函数是异步的。
async def parse(response):
return response.xpath('//meta[@name="title"]/@content')[0]
数据导出
上面函数的处理结果将在这个函数中统一被导出。下列例子为直接在控制台中打印导出结果。
保存需要IO操作,因此这个函数可能运行较慢,因此也需要是异步的。我们在前面添加async关键词
async def save(item):
print(item)
然后将这些模块组成一个Spider
首先导入爬虫对象:
import AsynSpider from simpyder.spiders
你可以这样组装Spider
spider = AsyncSpider()
spider.gen_url = gen_url
spider.parse = parse
spider.save = save
接着就可以开始爬虫任务
s.run()
你也可以通过构造函数进行一些配置
spider = AsyncSpider(name="TEST")
示例程序
from simpyder.spiders import AsynSpider
# new一个异步爬虫
s = AsynSpider()
# 定义链接生成的生成器,这里是爬取800次百度首页的爬虫
def g():
count = 0
while count < 800:
count += 1
yield "https://www.baidu.com"
# 绑定生成器
s.gen_url = g
# 定义用于解析的异步函数,这里不进行任何操作,返回一段文本
async def p(res):
return "parsed item"
# 绑定解析器
s.parse = p
# 定义用于存储的异步函数,这里不进行任何操作,但是返回2,表示解析出2个对象
async def s(item):
return 2
# 绑定存储器
s.save = s
# 运行
s.run()
理论速率
运行上述代码,可以得到单进程、并发数:64、仅进行计数操作的下载速率:
[2020-09-02 23:42:48,097][CRITICAL] @ Simpyder: user_agent: Simpyder ver.0.1.9
[2020-09-02 23:42:48,169][CRITICAL] @ Simpyder: concurrency: 64
[2020-09-02 23:42:48,244][CRITICAL] @ Simpyder: interval: 0
[2020-09-02 23:42:48,313][INFO] @ Simpyder: 已经爬取0个链接(0/min),共产生0个对象(0/min)
[2020-09-02 23:42:48,319][INFO] @ Simpyder: Start Crawler: 0
[2020-09-02 23:42:53,325][INFO] @ Simpyder: 已经爬取361个链接(4332/min),共产生658个对象(7896/min)
[2020-09-02 23:42:58,304][INFO] @ Simpyder: 已经爬取792个链接(5280/min),共产生1540个对象(10266/min)
[2020-09-02 23:43:03,304][INFO] @ Simpyder: 已经爬取1024个链接(4388/min),共产生2048个对象(8777/min)
[2020-09-02 23:43:05,007][CRITICAL] @ Simpyder: Simpyder任务执行完毕
[2020-09-02 23:43:05,008][CRITICAL] @ Simpyder: 累计消耗时间:0:00:16.695013
[2020-09-02 23:43:05,008][CRITICAL] @ Simpyder: 累计爬取链接:1024
[2020-09-02 23:43:05,009][CRITICAL] @ Simpyder: 累计生成对象:2048
- 该项目由@Jannchie维护
- 你可以通过邮箱jannchie@gmail.com进行联系
Metadata
Release files for simpyder 0.1.12
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| simpyder-0.1.12.tar.gz | 10.9 kB | Without provenance | Details for simpyder-0.1.12.tar.gz |
Release files / simpyder-0.1.12.tar.gz
| Download URL | simpyder-0.1.12.tar.gz |
|---|---|
| Size | 10.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
89e0aa188c5c39d169acb8b5c27c654cb10b85a43126d17ffd941363a2d84b0c
|
|
BLAKE2b-256 checksum How to use checksums |
c1eedd1aa3c965a57fcf5e881090f6941c148ecd592072c144a2e8aeee7cc5be
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.7.1
|
Security for simpyder 0.1.12
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.