Limiting/throttling the rate of HTTP requests in GRequests

前端 未结 4 1545
盖世英雄少女心
盖世英雄少女心 2020-12-23 14:46

I\'m writing a small script in Python 2.7.3 with GRequests and lxml that will allow me to gather some collectible card prices from various websites and compare them. Problem

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-23 15:36

    Take a look at this for automatic requests throttling: https://pypi.python.org/pypi/RequestsThrottler/0.2.2

    You can set both a fixed amount of delay between each request or set a number of requests to send in a fixed amount of seconds (which is basically the same thing):

    import requests
    from requests_throttler import BaseThrottler
    
    request = requests.Request(method='GET', url='http://www.google.com')
    reqs = [request for i in range(0, 5)]  # An example list of requests
    with BaseThrottler(name='base-throttler', delay=1.5) as bt:
        throttled_requests = bt.multi_submit(reqs)
    

    where the function multi_submit returns a list of ThrottledRequest (see doc: link at the end).

    You can then access to the responses:

    for tr in throttled_requests:
        print tr.response
    

    Alternatively you can achieve the same by specifying the number or requests to send in a fixed amount of time (e.g. 15 requests every 60 seconds):

    import requests
    from requests_throttler import BaseThrottler
    
    request = requests.Request(method='GET', url='http://www.google.com')
    reqs = [request for i in range(0, 5)]  # An example list of requests
    with BaseThrottler(name='base-throttler', reqs_over_time=(15, 60)) as bt:
        throttled_requests = bt.multi_submit(reqs)
    

    Both solutions can be implemented without the usage of the with statement:

    import requests
    from requests_throttler import BaseThrottler
    
    request = requests.Request(method='GET', url='http://www.google.com')
    reqs = [request for i in range(0, 5)]  # An example list of requests
    bt = BaseThrottler(name='base-throttler', delay=1.5)
    bt.start()
    throttled_requests = bt.multi_submit(reqs)
    bt.shutdown()
    

    For more details: http://pythonhosted.org/RequestsThrottler/index.html

提交回复
热议问题