添加了对媒体资源服务器的异常处理,参见 issue #691

This commit is contained in:
未来可欺
2025-08-05 13:11:00 +08:00
parent b9d30bbabb
commit 81f2dbe4ab
8 changed files with 50 additions and 24 deletions

View File

@@ -32,7 +32,7 @@ class XiaoHongShuClient(AbstractApiClient):
def __init__(
self,
timeout=30, # 若开启爬取媒体选项xhs 的长视频需要更久的超时时间
timeout=60, # 若开启爬取媒体选项xhs 的长视频需要更久的超时时间
proxy=None,
*,
headers: Dict[str, str],
@@ -152,12 +152,17 @@ class XiaoHongShuClient(AbstractApiClient):
async def get_note_media(self, url: str) -> Union[bytes, None]:
async with httpx.AsyncClient(proxy=self.proxy) as client:
response = await client.request("GET", url, timeout=self.timeout)
if not response.reason_phrase == "OK":
utils.logger.error(f"[XiaoHongShuClient.get_note_media] request {url} err, res:{response.text}")
try:
response = await client.request("GET", url, timeout=self.timeout)
response.raise_for_status()
if not response.reason_phrase == "OK":
utils.logger.error(f"[XiaoHongShuClient.get_note_media] request {url} err, res:{response.text}")
return None
else:
return response.content
except httpx.HTTPStatusError as exc: # some wrong when call httpx.request method, such as connection error, client error or server error
utils.logger.error(f"[DouYinClient.get_aweme_media] {exc}")
return None
else:
return response.content
async def pong(self) -> bool:
"""

View File

@@ -453,6 +453,7 @@ class XiaoHongShuCrawler(AbstractCrawler):
if not url:
continue
content = await self.xhs_client.get_note_media(url)
await asyncio.sleep(random.random())
if content is None:
continue
extension_file_name = f"{picNum}.jpg"
@@ -476,6 +477,7 @@ class XiaoHongShuCrawler(AbstractCrawler):
videoNum = 0
for url in videos:
content = await self.xhs_client.get_note_media(url)
await asyncio.sleep(random.random())
if content is None:
continue
extension_file_name = f"{videoNum}.mp4"