feat: kb-zhihu-metadata 支持 ZHIHU_COOKIE

- 安装 markdownify 依赖
- PlaywrightBrowser 支持传入 Cookie
- ZhihuPlaywrightExtractor 支持传入 Cookie
- zhihu_metadata.py 读取 ZHIHU_COOKIE 环境变量
- 更新 SKILL.md 添加 Cookie 配置说明
This commit is contained in:
2026-02-27 15:39:19 +08:00
parent 202960eb64
commit 85797d0a89
3 changed files with 76 additions and 22 deletions
+35 -7
View File
@@ -30,16 +30,42 @@ class PlaywrightBrowser:
self.context = None
self.page = None
def start(self):
def start(self, cookie: str = None):
"""启动浏览器"""
from playwright.sync_api import sync_playwright
self.playwright = sync_playwright().start()
self.browser = self.playwright.chromium.launch(headless=self.headless)
self.context = self.browser.new_context(
user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36'
)
# 创建浏览器上下文
context_options = {
'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36'
}
# 如果提供了 Cookie,设置 Cookie
if cookie:
context_options['cookies'] = self._parse_cookie(cookie)
self.context = self.browser.new_context(**context_options)
self.page = self.context.new_page()
self.page.set_default_timeout(self.timeout)
def _parse_cookie(self, cookie_str: str) -> list:
"""解析 Cookie 字符串为 Playwright 格式"""
cookies = []
for item in cookie_str.split(';'):
item = item.strip()
if '=' in item:
name, value = item.split('=', 1)
cookies.append({
'name': name.strip(),
'value': value.strip(),
'domain': '.zhihu.com',
'path': '/',
'secure': True,
'httpOnly': False,
'sameSite': 'Lax'
})
return cookies
def close(self):
"""关闭浏览器"""
@@ -84,14 +110,16 @@ class ZhihuPlaywrightExtractor:
'question': r'zhihu\.com/question/(\d+)',
}
def __init__(self, headless: bool = True, timeout: int = 30):
def __init__(self, headless: bool = True, timeout: int = 30, cookie: str = None):
"""
初始化提取器
Args:
headless: 是否使用无头浏览器
timeout: 超时时间(秒)
cookie: 知乎 Cookie 字符串(可选)
"""
self.cookie = cookie
self.browser = PlaywrightBrowser(headless=headless, timeout=timeout)
self.markdown_converter = MarkdownConverter()
@@ -383,8 +411,8 @@ class ZhihuPlaywrightExtractor:
print(f"URL 类型: {url_type}")
print(f"启动浏览器...")
# 启动浏览器
self.browser.start()
# 启动浏览器(传入 Cookie)
self.browser.start(cookie=self.cookie)
print(f"访问页面: {url}")
# 访问页面