Skip to content

Commit e0fbd79

Browse files
committed
perf: 优化提取链接的正则表达式
Closes #261
1 parent b63f546 commit e0fbd79

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

source/application/app.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,12 @@ class XHS:
7474
VERSION_MAJOR = VERSION_MAJOR
7575
VERSION_MINOR = VERSION_MINOR
7676
VERSION_BETA = VERSION_BETA
77-
LINK = compile(r"https?://www\.xiaohongshu\.com/explore/\S+")
78-
USER = compile(r"https?://www\.xiaohongshu\.com/user/profile/[a-z0-9]+/\S+")
79-
SHARE = compile(r"https?://www\.xiaohongshu\.com/discovery/item/\S+")
80-
SHORT = compile(r"https?://xhslink\.com/[^\s\"<>\\^`{|},。;!?、【】《》]+")
77+
LINK = compile(r"(?:https?://)?www\.xiaohongshu\.com/explore/\S+")
78+
USER = compile(r"(?:https?://)?www\.xiaohongshu\.com/user/profile/[a-z0-9]+/\S+")
79+
SHARE = compile(r"(?:https?://)?www\.xiaohongshu\.com/discovery/item/\S+")
80+
SHORT = compile(r"(?:https?://)?xhslink\.com/[^\s\"<>\\^`{|},。;!?、【】《》]+")
8181
ID = compile(r"(?:explore|item)/(\S+)?\?")
82+
ID_USER = compile(r"user/profile/[a-z0-9]+/(\S+)?\?")
8283
__INSTANCE = None
8384
CLEANER = Cleaner()
8485

@@ -286,6 +287,8 @@ def extract_id(self, links: list[str]) -> list[str]:
286287
for i in links:
287288
if j := self.ID.search(i):
288289
ids.append(j.group(1))
290+
elif j := self.ID_USER.search(i):
291+
ids.append(j.group(1))
289292
return ids
290293

291294
async def __deal_extract(

source/application/request.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ async def request_url(
3232
proxy: str = None,
3333
**kwargs,
3434
) -> str:
35+
if not url.startswith("http"):
36+
url = f"https://{url}"
3537
headers = self.update_cookie(
3638
cookie,
3739
)

0 commit comments

Comments
 (0)