2022-09-23
docs
插件开发
如何编写自己的解析插件
- example:
# name: site_donate_name.py
# author: xxx
# version: 0.0.1
# date: xxx
# desc: xxx
from plugins import BaseParser
class Parser(BaseParser):
def __init__(self, *args, **kwargs):
super(Parser, self).__init__(*args, **kwargs)
# 指定改解析域名的友好名称,作为该域名的主目录. (可选)
self.friend_name = "xxx"
# 重写route函数,根据输入的url判定解析规则
def route(self, *args, **kwargs):
html = kwargs.get("html")
response = kwargs.get("response")
# 根据表达式规则或其他规则进行分类解析
if re.match("/(discovery|follow)/?", url_path):
name = re.match("/(discovery|follow)/?", url_path).group(1)
self.task_name = f"{name.capitalize()}"
return self.call_parse(self.parse_xxx, parse_args, parse_kwargs)
elif ...
def parse_xxx(self, *args, **kwargs)
<your_parse_code>
item_data = {
# 必要键值 -> item下载url
"item_url": item_url,
# 可选键值 -> item额外属性
"name": str,
"sub_dir": str,
"size": (width(int), height(int)),
"bytes": int,
"type": str,
"item_index_reset": bool,
# 中止键值 -> 用于主动中止解析
"is_abort": True,
"abort_msg": str
}
yield item_data
- 将完成的
文件放入'./plugins/'目录中. - Done!