Skip to main content

A lightweight network request framework

Project description

async-request

A lightweight network request framework based on requests & asyncio

install

pip install async_request

usage

from async_request import Request, crawl


def parse_baidu(response):
    print(response.url, response.status_code)
    yield Request('https://cn.bing.com/', callback=parse_bing)


def parse_bing(response):
    print(response.url, response.status_code)
    print(response.xpath('//a/@href').get())
    yield Request('https://github.com/financialfly/async-request', callback=parse_github)


def parse_github(response):
    print(response.url, response.status_code)
    yield {'hello': 'github'}


def process_result(result):
    print(result)


if __name__ == '__main__':
    request_list = [Request(url='https://www.google.com', callback=parse_baidu)]
    crawl(request_list, result_callback=process_result)

And you'll see the result like this:

https://www.baidu.com/ 200
https://cn.bing.com/ 200
javascript:void(0)
https://github.com/financialfly/async-request 200
{'hello': 'github'}

test

Use fetch function to get a response immediately:

from async_request import fetch

def parse():
    response = fetch('https://www.bing.com')
    print(response)
    
   
if __name__ == '__main__':
    parse()

the output will like this:

<async_request.Response 200 https://cn.bing.com/>

Use the test decorator is also a method to test spider:

import async_request as ar


@ar.test('https://www.baidu.com')
def parse(response):
    print(response.url, response.status_code)
    
    
if __name__ == '__main__':
    parse()

then run the script, you will see the result:

https://www.baidu.com/ 200

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

async-request-0.1618.tar.gz (5.1 kB view details)

Uploaded Source

File details

Details for the file async-request-0.1618.tar.gz.

File metadata

  • Download URL: async-request-0.1618.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.7.2

File hashes

Hashes for async-request-0.1618.tar.gz
Algorithm Hash digest
SHA256 597df420817b410c2c10707f40155d1d3717c48a18a54a5a9d63c703ed9bbe85
MD5 3ce5e366fe103876482d02afb0b1b64f
BLAKE2b-256 0a748863935a1f8643dcb5deca1d4c02b780e47e92d733d629fcfecca0eddc58

See more details on using hashes here.

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