Skip to content

Commit d4d3d9e

Browse files
DAFT-9: Add retries to requests (TheJokersThief#9)
1 parent 8ad6792 commit d4d3d9e

File tree

4 files changed

+32
-12
lines changed

4 files changed

+32
-12
lines changed

daft_scraper/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import requests
22
from bs4 import BeautifulSoup
33
from urllib.parse import urljoin
4+
from urllib3.util import Retry
45

56

67
class DaftHTTPSession(requests.Session):
@@ -29,9 +30,17 @@ class Daft():
2930
"""
3031
BASE_URL = "https://www.daft.ie/"
3132

32-
def __init__(self, user_agent=None):
33+
def __init__(self, user_agent=None, enable_retries=True):
3334
self.site = DaftHTTPSession(Daft.BASE_URL, user_agent=user_agent)
3435

36+
if enable_retries:
37+
retry_strategy = Retry(
38+
total=3,
39+
backoff_factor=0.1
40+
)
41+
adapter = requests.adapters.HTTPAdapter(max_retries=retry_strategy)
42+
self.site.mount("https://", adapter)
43+
3544
def get(self, url, params=None):
3645
req = self.site.get(url, params=params)
3746
req.raise_for_status()

daft_scraper/search/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def search(self, query: List[Option], max_pages: int = sys.maxsize):
3939
path = path.replace('ireland', locations[0])
4040
del options['location']
4141

42+
print(path, options)
4243
# Init pagination params
4344
options['pageSize'] = self.PAGE_SIZE
4445
options['from'] = 0

poetry.lock

Lines changed: 19 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "daft-scraper"
3-
version = "1.0.4"
3+
version = "1.1.0"
44
description = "A webscraper for Daft.ie"
55
authors = ["Evan Smith <[email protected]>"]
66
license = "MIT"
@@ -16,6 +16,7 @@ requests = "^2.25.0"
1616
typer = "^0.3.2"
1717
marshmallow = "^3.9.1"
1818
tabulate = "^0.8.7"
19+
urllib3 = "^1.26.2"
1920

2021
[tool.poetry.dev-dependencies]
2122
flake8 = "^3.8.4"

0 commit comments

Comments
 (0)