fix: return bare Google URL in referer instead of search query (#179)
Browse files
scrapling/engines/toolbelt/fingerprints.py
CHANGED
|
@@ -20,13 +20,13 @@ chrome_version = 143
|
|
| 20 |
|
| 21 |
@lru_cache(10, typed=True)
|
| 22 |
def generate_convincing_referer(url: str) -> str | None:
|
| 23 |
-
"""
|
| 24 |
|
| 25 |
>>> generate_convincing_referer('https://www.somewebsite.com/blah')
|
| 26 |
-
'https://www.google.com/
|
| 27 |
|
| 28 |
:param url: The URL you are about to fetch.
|
| 29 |
-
:return: Google's
|
| 30 |
"""
|
| 31 |
# Fixing the inaccurate return type hint in `get_tld`
|
| 32 |
extracted: Result | None = cast(Result, get_tld(url, as_object=True, fail_silently=True))
|
|
@@ -43,7 +43,7 @@ def generate_convincing_referer(url: str) -> str | None:
|
|
| 43 |
if all(part.isdigit() for part in website_name.split(".") if part):
|
| 44 |
return None
|
| 45 |
|
| 46 |
-
return
|
| 47 |
|
| 48 |
|
| 49 |
@lru_cache(1, typed=True)
|
|
|
|
| 20 |
|
| 21 |
@lru_cache(10, typed=True)
|
| 22 |
def generate_convincing_referer(url: str) -> str | None:
|
| 23 |
+
"""Generate a convincing Google referer header
|
| 24 |
|
| 25 |
>>> generate_convincing_referer('https://www.somewebsite.com/blah')
|
| 26 |
+
'https://www.google.com/'
|
| 27 |
|
| 28 |
:param url: The URL you are about to fetch.
|
| 29 |
+
:return: Google's URL as referer, or None for localhost/IP addresses
|
| 30 |
"""
|
| 31 |
# Fixing the inaccurate return type hint in `get_tld`
|
| 32 |
extracted: Result | None = cast(Result, get_tld(url, as_object=True, fail_silently=True))
|
|
|
|
| 43 |
if all(part.isdigit() for part in website_name.split(".") if part):
|
| 44 |
return None
|
| 45 |
|
| 46 |
+
return "https://www.google.com/"
|
| 47 |
|
| 48 |
|
| 49 |
@lru_cache(1, typed=True)
|
tests/fetchers/test_utils.py
CHANGED
|
@@ -209,8 +209,7 @@ class TestFingerprintFunctions:
|
|
| 209 |
url = "https://sub.example.com/page.html"
|
| 210 |
result = generate_convincing_referer(url)
|
| 211 |
|
| 212 |
-
assert result
|
| 213 |
-
assert "example" in result
|
| 214 |
|
| 215 |
def test_generate_convincing_referer_caching(self):
|
| 216 |
"""Test referer generation caching"""
|
|
|
|
| 209 |
url = "https://sub.example.com/page.html"
|
| 210 |
result = generate_convincing_referer(url)
|
| 211 |
|
| 212 |
+
assert result == "https://www.google.com/"
|
|
|
|
| 213 |
|
| 214 |
def test_generate_convincing_referer_caching(self):
|
| 215 |
"""Test referer generation caching"""
|