新增作品链接格式判断

This commit is contained in:
JoeamAmier 2023-08-28 21:39:44 +08:00
parent 021959c8fe
commit 0612dbd39f
2 changed files with 10 additions and 0 deletions

View File

@ -4,6 +4,7 @@ from source import XHS
def example():
"""使用示例"""
# 测试链接
error_demo = "https://www.xiaohongshu.com/explore/"
image_demo = "https://www.xiaohongshu.com/explore/64d1b406000000000103ee8d"
video_demo = "https://www.xiaohongshu.com/explore/64c05652000000000c0378e7"
# 实例对象
@ -26,6 +27,7 @@ def example():
# 无需区分图文和视频作品
# 返回作品详细数据,包括下载地址
download = True # 启用自动下载作品文件
print(xhs.extract(error_demo)) # 获取数据失败时返回空字典
print(xhs.extract(image_demo, download=download))
print(xhs.extract(video_demo, download=download))

View File

@ -1,3 +1,5 @@
from re import compile
from .Download import Download
from .Explore import Explore
from .Html import Html
@ -11,6 +13,7 @@ class XHS:
"Referer": "https://www.xiaohongshu.com/",
"Cookie": "abRequestId=27dafe41-28af-5b33-9f22-fe05d8c4ac2f; xsecappid=xhs-pc-web; a1=18a363d90c9gw7eaz2krqhj4cx2gtwgotul1wur8950000289463; webId=27fb29ed7ff41eadd4bc58197a465b63; websectiga=cffd9dcea65962b05ab048ac76962acee933d26157113bb213105a116241fa6c; sec_poison_id=3a1e34ee-3535-4ee9-8186-4d574da5291e; web_session=030037a3d84590608f6da85793234a9a6588ed; gid=yY0qKqfd2Y9qyY0qKqfj877FSjkEWd0uJTFA1YjxV4SCJy28k9EklE888JYj4Kq82242dKiY; webBuild=3.6.0; cache_feeds=[]",
}
links = compile(r"https://www.xiaohongshu.com/explore/[0-9a-z]+")
def __init__(
self,
@ -44,6 +47,8 @@ class XHS:
container["下载地址"] = url
def extract(self, url: str, download=False) -> dict:
if not self.check(url):
return {}
html = self.html.get_html(url)
if not html:
return {}
@ -53,3 +58,6 @@ class XHS:
else:
self.get_image(data, html, download)
return data
def check(self, url: str):
return self.links.match(url)