Skip to content

Commit

Permalink
Add retries for both scrapers
Browse files Browse the repository at this point in the history
  • Loading branch information
Javex committed Sep 27, 2023
1 parent a844cb9 commit 0485ab9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
4 changes: 2 additions & 2 deletions hotprices_au/coles.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from datetime import datetime
from bs4 import BeautifulSoup

from . import output
from . import output, request


class ColesScraper:
Expand All @@ -13,7 +13,7 @@ def __init__(self, store_id, quick=False):
self.quick = quick
self.store_id = store_id

self.session = requests.Session()
self.session = request.get_base_session()
self.session.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
'Origin': 'https://www.coles.com.au',
Expand Down
16 changes: 16 additions & 0 deletions hotprices_au/request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import requests
from requests.adapters import HTTPAdapter
from urllib3 import Retry


def get_base_session():
session = requests.Session()
retry = Retry(
total=10,
backoff_factor=1,
backoff_max=60,
status_forcelist=[403],
allowed_methods=['GET', 'POST'],
)
session.mount('https://', HTTPAdapter(max_retries=retry))
return session
4 changes: 2 additions & 2 deletions hotprices_au/woolies.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import requests
import json

from . import output
from . import output, request


class WooliesAPI:
Expand All @@ -10,7 +10,7 @@ def __init__(self, quick=False):
self.quick = quick


self.session = requests.Session()
self.session = request.get_base_session()
self.session.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
'Origin': 'https://www.woolworths.com.au',
Expand Down

0 comments on commit 0485ab9

Please sign in to comment.