Skip to content

Commit 4a3284e

Browse files
committed
🔄 refactor: add headers configuration to the HTTP request wrapper
1 parent 0e452e6 commit 4a3284e

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

app/crawler/utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
import httpx
66
from loguru import logger
77

8+
headers = {
9+
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0",
10+
}
11+
812

913
async def multi_http_request(
1014
multi_requests: Dict,
@@ -46,7 +50,7 @@ async def multi_http_request(
4650
f"requests_list={[(key, response_mapper[key]) for key, request in requests_list]}"
4751
)
4852
await asyncio.sleep(wait_time)
49-
async with httpx.AsyncClient() as client:
53+
async with httpx.AsyncClient(headers=headers) as client:
5054
tasks = [client.request(**request) for key, request in requests_list]
5155
response_list = await asyncio.gather(*tasks, return_exceptions=True)
5256
wait_time = 0
@@ -58,6 +62,7 @@ async def multi_http_request(
5862
# response could be an Exception here
5963
logger.warning(
6064
f"multi_http_request error: {request=} "
65+
f"response.status_code: "
6166
f"{response.status_code if isinstance(response, httpx.Response) else response}"
6267
)
6368
response_mapper[key] += 1

0 commit comments

Comments
 (0)