retries
parameter in the constructor to enable this feature and enhance your scraping efficiency.Retry
class from urllib3
and the HTTPAdapter
from requests. These are part of the requests
library, so you don’t need to install them separately.
Instead of making direct get
calls, we create a requests session. This approach allows us to set up and reuse the session configuration, including retry settings, for all subsequent requests. This setup is efficient and ensures consistent retry behavior across all requests.
You can adjust the Retry
parameters to suit your needs. Here are some key parameters:
total
sets the maximum number of retries allowed.backoff_factor
defines the delay between retries using an exponential backoff strategy. The formula is: . For example, a backoff factor of 1 results in delays of 1, 2, and 4 seconds for three retries.status_forcelist
is a list of HTTP status codes that will force a retry.axios
, we’ll use the axiosRetry library to manage retries. This configuration will automatically retry requests for all axios
calls, and you can also create a specific axios client
with retry capabilities.
You can adjust the axiosRetry
parameters to fit your specific requirements. The configuration provided below should work well for most scenarios:
retries
sets the number of allowed retries.retryDelay
this applies an exponential delay between attempts, with an additional 0-20% random delay margin to prevent repeated requests from hitting the server at regular intervals.retryCondition
this function determines whether an error is eligible for a retry. By default, the function retries only 5xx errors. However, in our example, we’ve added a check for 429 (Too Many Requests) errors to ensure these are also retried.