Code Monkey home page Code Monkey logo

scrapy_utils's Introduction

scrapy_utils

Description

Utilities for scrapy

Module

Middleware

GithubOAuthMiddleware

Make OAuth authentication with Github API token

Extension

GithubRateLimitWatcher

Watch GitHub Rate Limit in GitHub API header and close the connection for crawling if Rate Limit is over the limit user set in Scrapy setting file.

Usage

GithubOAuthMiddleware

Prerequisite

Get Github API OAuth token. Please refer to Github Developer OAuth or Github Developer Getting Started

Setup

Create the middlewares directory under your spider folder.

mkdir -p <your_spider>/middlewares

Copy github_oauth.py under the middlewares directory.

git clone https://github.com/yu74n/scrapy_utils.git
cp scrapy/downloadermiddlewares/github_oauth.py <your_spider>/middlewares

Open your spider's setting.

vi <your_spider>/settings.py

Add Middleware name to DOWNLOADER_MIDDLEWARES in settings.py like

DOWNLOADER_MIDDLEWARES = {
    '<your_spider>.middlewares.github_oauth.GithubOAuthMiddleware': 290,
}

Set your Github API token to oauth_token variable in your spider code

class YourSpider(scrapy.Spider):
    ...
    oauth_token = "xxxxxxxxxxxxxxxxxxxxxxxxx"
    ...
    def start_requests(self):
        ...

GithubRateLimitWatcher

Setup

Create the extensions directory under your spider folder.

mkdir -p <your_spider>/extensions

Copy github_oauth.py under the extensions directory.

git clone https://github.com/yu74n/scrapy_utils.git
cp scrapy/extensions/github_rate_limit_watcher.py <your_spider>/extensions

Open your spider's setting.

vi <your_spider>/settings.py

Add Extension name to EXTENSIONS in settings.py like

EXTENSIONS = {
    '<your_spider>.extensions.github_rate_limit_watcher.GithubRateLimitWatcher': 500,
}

Setting

GITHUB_RATE_LIMIT_WATCHER_LIMITCOUNT

X-RateLimit-Remaining(please refer to Rate Limit) describes the number of requests remaining you can send with GitHub API.
When X-RateLimit-Remaining reaches GITHUB_RATE_LIMIT_WATCHER_LIMITCOUNT, your crawler will be closed.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.