mirror of
https://github.com/JoeanAmier/XHS-Downloader.git
synced 2026-03-22 06:57:16 +08:00
优化代码
This commit is contained in:
10
README.md
10
README.md
@@ -17,17 +17,15 @@ video_demo = "https://www.xiaohongshu.com/explore/64c05652000000000c0378e7"
|
|||||||
# 实例对象
|
# 实例对象
|
||||||
path = "./" # 作品下载储存根路径,默认值:当前路径
|
path = "./" # 作品下载储存根路径,默认值:当前路径
|
||||||
folder = "Download" # 作品下载文件夹名称(自动创建),默认值:Download
|
folder = "Download" # 作品下载文件夹名称(自动创建),默认值:Download
|
||||||
headers = {
|
|
||||||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 Edg/115.0.1901.203",
|
|
||||||
} # 请求头
|
|
||||||
proxies = None # 代理
|
proxies = None # 代理
|
||||||
timeout = 10 # 网络请求超时限制,默认值:10
|
timeout = 5 # 网络请求超时限制,默认值:10
|
||||||
|
chunk = 1024 * 1024 # 下载文件时,每次从服务器获取的数据块大小,单位字节
|
||||||
xhs = XHS(
|
xhs = XHS(
|
||||||
path=path,
|
path=path,
|
||||||
folder=folder,
|
folder=folder,
|
||||||
headers=headers,
|
|
||||||
proxies=proxies,
|
proxies=proxies,
|
||||||
timeout=timeout,) # 使用自定义参数
|
timeout=timeout,
|
||||||
|
chunk=chunk, ) # 使用自定义参数
|
||||||
# xhs = XHS() # 使用默认参数
|
# xhs = XHS() # 使用默认参数
|
||||||
# 无需区分图文和视频作品
|
# 无需区分图文和视频作品
|
||||||
# 返回作品详细数据,包括下载地址
|
# 返回作品详细数据,包括下载地址
|
||||||
|
|||||||
10
main.py
10
main.py
@@ -10,17 +10,15 @@ def example():
|
|||||||
# 实例对象
|
# 实例对象
|
||||||
path = "./" # 作品下载储存根路径,默认值:当前路径
|
path = "./" # 作品下载储存根路径,默认值:当前路径
|
||||||
folder = "Download" # 作品下载文件夹名称(自动创建),默认值:Download
|
folder = "Download" # 作品下载文件夹名称(自动创建),默认值:Download
|
||||||
headers = {
|
|
||||||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 Edg/115.0.1901.203",
|
|
||||||
} # 请求头
|
|
||||||
proxies = None # 代理
|
proxies = None # 代理
|
||||||
timeout = 10 # 网络请求超时限制,默认值:10
|
timeout = 5 # 网络请求超时限制,默认值:10
|
||||||
|
chunk = 1024 * 1024 # 下载文件时,每次从服务器获取的数据块大小,单位字节
|
||||||
xhs = XHS(
|
xhs = XHS(
|
||||||
path=path,
|
path=path,
|
||||||
folder=folder,
|
folder=folder,
|
||||||
headers=headers,
|
|
||||||
proxies=proxies,
|
proxies=proxies,
|
||||||
timeout=timeout, ) # 使用自定义参数
|
timeout=timeout,
|
||||||
|
chunk=chunk, ) # 使用自定义参数
|
||||||
# xhs = XHS() # 使用默认参数
|
# xhs = XHS() # 使用默认参数
|
||||||
# 无需区分图文和视频作品
|
# 无需区分图文和视频作品
|
||||||
# 返回作品详细数据,包括下载地址
|
# 返回作品详细数据,包括下载地址
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ class Download:
|
|||||||
proxies=None,
|
proxies=None,
|
||||||
chunk=256 * 1024, ):
|
chunk=256 * 1024, ):
|
||||||
self.root = self.init_root(path, folder)
|
self.root = self.init_root(path, folder)
|
||||||
self.headers = self.init_headers(headers)
|
self.headers = headers
|
||||||
self.proxies = {
|
self.proxies = {
|
||||||
"http": proxies,
|
"http": proxies,
|
||||||
"https": proxies,
|
"https": proxies,
|
||||||
@@ -22,10 +22,6 @@ class Download:
|
|||||||
}
|
}
|
||||||
self.chunk = chunk
|
self.chunk = chunk
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def init_headers(headers: dict) -> dict:
|
|
||||||
return {"User-Agent": headers["User-Agent"]}
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def init_root(path: str, folder: str) -> Path:
|
def init_root(path: str, folder: str) -> Path:
|
||||||
root = Path(path).joinpath(folder)
|
root = Path(path).joinpath(folder)
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ class Html:
|
|||||||
headers: dict,
|
headers: dict,
|
||||||
proxies=None,
|
proxies=None,
|
||||||
timeout=10, ):
|
timeout=10, ):
|
||||||
self.headers = headers
|
self.headers = headers | {"Referer": "https://www.xiaohongshu.com/", }
|
||||||
self.proxies = {
|
self.proxies = {
|
||||||
"http": proxies,
|
"http": proxies,
|
||||||
"https": proxies,
|
"https": proxies,
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ from .Video import Video
|
|||||||
class XHS:
|
class XHS:
|
||||||
headers = {
|
headers = {
|
||||||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36",
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36",
|
||||||
"Referer": "https://www.xiaohongshu.com/",
|
|
||||||
}
|
}
|
||||||
links = compile(r"https://www.xiaohongshu.com/explore/[0-9a-z]+")
|
links = compile(r"https://www.xiaohongshu.com/explore/[0-9a-z]+")
|
||||||
|
|
||||||
@@ -20,14 +19,15 @@ class XHS:
|
|||||||
self,
|
self,
|
||||||
path="./",
|
path="./",
|
||||||
folder="Download",
|
folder="Download",
|
||||||
headers=None,
|
|
||||||
proxies=None,
|
proxies=None,
|
||||||
timeout=10):
|
timeout=10,
|
||||||
self.html = Html(headers or self.headers, proxies, timeout)
|
chunk=256 * 1024,
|
||||||
|
):
|
||||||
|
self.html = Html(self.headers, proxies, timeout)
|
||||||
self.image = Image()
|
self.image = Image()
|
||||||
self.video = Video()
|
self.video = Video()
|
||||||
self.explore = Explore()
|
self.explore = Explore()
|
||||||
self.download = Download(path, folder, self.html.headers, proxies)
|
self.download = Download(path, folder, self.headers, proxies, chunk)
|
||||||
|
|
||||||
def get_image(self, container: dict, html: str, download):
|
def get_image(self, container: dict, html: str, download):
|
||||||
urls = self.image.get_image_link(html)
|
urls = self.image.get_image_link(html)
|
||||||
|
|||||||
Reference in New Issue
Block a user