mirror of
https://github.com/JoeanAmier/XHS-Downloader.git
synced 2026-03-22 06:57:16 +08:00
新增批量下载功能
This commit is contained in:
@@ -50,6 +50,8 @@ print(xhs.extract(error_demo)) # 获取数据失败时返回空字典
|
|||||||
print(xhs.extract(image_demo, download=download))
|
print(xhs.extract(image_demo, download=download))
|
||||||
print(xhs.extract(video_demo, download=download))
|
print(xhs.extract(video_demo, download=download))
|
||||||
</pre>
|
</pre>
|
||||||
|
<h1>⛓ 批量下载</h1>
|
||||||
|
<p>在程序当前文件夹创建一个 <code>xhs.txt</code> 文本文件,然后将待处理的作品链接输入文件,每行输入一个作品链接,编辑完成后保存文件,然后运行程序,程序会自动读取 <code>xhs.txt</code> 文件内容,并批量下载每个作品的文件,下载完成后需要手动删除 <code>xhs.txt</code> 文件。</p>
|
||||||
<h1>⚙️ 配置文件</h1>
|
<h1>⚙️ 配置文件</h1>
|
||||||
<p>根目录下的 <code>settings.json</code> 文件,可以自定义部分运行参数。</p>
|
<p>根目录下的 <code>settings.json</code> 文件,可以自定义部分运行参数。</p>
|
||||||
<table>
|
<table>
|
||||||
|
|||||||
60
main.py
60
main.py
@@ -1,3 +1,14 @@
|
|||||||
|
from textual.app import App
|
||||||
|
from textual.app import ComposeResult
|
||||||
|
from textual.binding import Binding
|
||||||
|
from textual.widgets import Button
|
||||||
|
from textual.widgets import Footer
|
||||||
|
from textual.widgets import Header
|
||||||
|
from textual.widgets import Input
|
||||||
|
from textual.widgets import Label
|
||||||
|
from textual.widgets import Static
|
||||||
|
|
||||||
|
from source import Batch
|
||||||
from source import Settings
|
from source import Settings
|
||||||
from source import XHS
|
from source import XHS
|
||||||
|
|
||||||
@@ -31,13 +42,52 @@ def example():
|
|||||||
def main():
|
def main():
|
||||||
"""读取并应用配置文件设置的参数,适合一般作品文件下载需求"""
|
"""读取并应用配置文件设置的参数,适合一般作品文件下载需求"""
|
||||||
xhs = XHS(**Settings().run())
|
xhs = XHS(**Settings().run())
|
||||||
while True:
|
if ids := Batch().read_txt():
|
||||||
if url := input("请输入小红书作品链接:"):
|
for i in ids:
|
||||||
xhs.extract(url, download=True)
|
xhs.extract(i, download=True)
|
||||||
else:
|
else:
|
||||||
break
|
while True:
|
||||||
|
if url := input("请输入小红书作品链接:"):
|
||||||
|
xhs.extract(url, download=True)
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
|
||||||
|
|
||||||
|
class RunMenu(Static):
|
||||||
|
|
||||||
|
def compose(self) -> ComposeResult:
|
||||||
|
yield Button("获取数据", id="run", variant="success")
|
||||||
|
yield Button("清空输入", id="reset")
|
||||||
|
|
||||||
|
|
||||||
|
class XHSDownloader(App):
|
||||||
|
CSS_PATH = "static/XHS_Downloader.tcss"
|
||||||
|
BINDINGS = [
|
||||||
|
Binding(key="q", action="quit", description="结束运行"),
|
||||||
|
Binding(
|
||||||
|
key="w",
|
||||||
|
action="repository",
|
||||||
|
description="获取源码",
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
||||||
|
def compose(self) -> ComposeResult:
|
||||||
|
yield Label("请输入小红书图文/视频作品链接:")
|
||||||
|
yield Input(placeholder="URL")
|
||||||
|
yield RunMenu()
|
||||||
|
yield Header()
|
||||||
|
yield Footer()
|
||||||
|
|
||||||
|
def on_mount(self) -> None:
|
||||||
|
self.title = "小红书作品采集工具"
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def action_repository():
|
||||||
|
yield Label("Github Repository")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# example()
|
# example()
|
||||||
main()
|
main()
|
||||||
|
# app = XHSDownloader()
|
||||||
|
# app.run()
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ from json import dump
|
|||||||
from json import load
|
from json import load
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
__all__ = ['Settings']
|
__all__ = ['Settings', 'Batch']
|
||||||
|
|
||||||
|
|
||||||
class Settings:
|
class Settings:
|
||||||
@@ -26,3 +26,13 @@ class Settings:
|
|||||||
with self.path.open("w", encoding="utf-8") as f:
|
with self.path.open("w", encoding="utf-8") as f:
|
||||||
dump(self.default, f, indent=2)
|
dump(self.default, f, indent=2)
|
||||||
return self.default
|
return self.default
|
||||||
|
|
||||||
|
|
||||||
|
class Batch:
|
||||||
|
file = Path("./xhs.txt")
|
||||||
|
|
||||||
|
def read_txt(self) -> list:
|
||||||
|
if self.file.is_file():
|
||||||
|
with self.file.open("r") as f:
|
||||||
|
return f.readlines()
|
||||||
|
return []
|
||||||
|
|||||||
@@ -4,10 +4,11 @@ from .Download import Download
|
|||||||
from .Explore import Explore
|
from .Explore import Explore
|
||||||
from .Html import Html
|
from .Html import Html
|
||||||
from .Image import Image
|
from .Image import Image
|
||||||
|
from .Settings import Batch
|
||||||
from .Settings import Settings
|
from .Settings import Settings
|
||||||
from .Video import Video
|
from .Video import Video
|
||||||
|
|
||||||
__all__ = ['XHS', 'Settings']
|
__all__ = ['XHS', 'Settings', 'Batch']
|
||||||
|
|
||||||
|
|
||||||
class XHS:
|
class XHS:
|
||||||
@@ -49,6 +50,7 @@ class XHS:
|
|||||||
|
|
||||||
def extract(self, url: str, download=False) -> dict:
|
def extract(self, url: str, download=False) -> dict:
|
||||||
if not self.__check(url):
|
if not self.__check(url):
|
||||||
|
print(f"无效的作品链接: {url}")
|
||||||
return {}
|
return {}
|
||||||
html = self.html.get_html(url)
|
html = self.html.get_html(url)
|
||||||
if not html:
|
if not html:
|
||||||
|
|||||||
Reference in New Issue
Block a user