Skip to main content

Distributed multithreading universal crawler

Project description

Simpyder - Simple Multithreaded Python Spider

Simpyder - 轻量级多线程Python爬虫

特点

  • 轻量级:下载便利,使用简单。
  • 多线程:并行下载解析,快速获取数据。
  • 可定制:简单配置,适应各种爬取场合。

快速开始

下载

#使用pip3
pip3 install simpyder --user
# 更新包
pip3 install simpyder --upgrade

编码

用户只需要定义三个函数,实现三个模块:

链接获取

我们需要一个定义一个生成器,用于产生链接。

def gen_url():
    for each_id in range(100):
        yield "https://www.biliob.com/api/video/{}".format(each_id)

链接解析

我们需要定义一个解析链接的函数。其中第一个参数是Response对象,也就是上述函数对应URL的访问结果。

该函数需要返回一个对象,作为处理结果。

def parse(response):
    return response.xpath('//meta[@name="title"]/@content')[0]

数据导出

上面函数的处理结果将在这个函数中统一被导出。下列例子为直接在控制台中打印导出结果。

def save(item):
    print(item)

然后将这些模块组成一个Spider

首先导入爬虫对象:

import Spider from simpyder

你可以使用构造函数组装Spider

s = Spider(gen_url, parse, save, name="DEMO") # 构造函数方式组装

也可以使用assemble函数进行组装

s = Spider()
s.assemble(gen_url, parse, save, name="DEMO") # 先创建爬虫对象,再装载各个模块

接着就可以开始爬虫任务

s.run()

你也可以配置进行一些简单的配置

from simpyder import SimpyderConfig
sc = SimpyderConfig
sc.COOKIES = "example:value"
sc.USER_AGENT = "my user agent"
s.assemble(gen_url=gen_url, parse=parse, save=save, name="DEMO",config=sc)

示例程序

参见B站视频标题爬虫


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simpyder-0.0.14.tar.gz (5.9 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page