import gradio as gr from bs4 import BeautifulSoup from markdownify import MarkdownConverter from playwright.sync_api import sync_playwright def md(soup, **options): return MarkdownConverter(**options).convert_soup(soup) def main_fn(url: str, check: list[int], request: gr.Request): user_agent = request.headers["user-agent"] with sync_playwright() as p: browser = p.chromium.launch( args=[ "--single-process", "--no-zygote", "--no-sandbox", "--disable-gpu", "--disable-dev-shm-usage", "--headless=new", ] ) context = browser.new_context(user_agent=user_agent) page = context.new_page() response = page.goto(url=url) status = response.status content = page.content() title = page.title() browser.close() soup = BeautifulSoup(content, features="html.parser") for tag in ["script", "style"]: target = soup.find_all(tag) for t in target: t.clear body = soup.find("body") main = soup.find("main") body = md(body) if main: body = md(main, strip=check) return f"{title}\n======\n\n{body}" demo = gr.Interface( main_fn, title="URL to Markdown", description="""
Gets HTML given by URL and converts it to Markdown.Does not support dynamically generated HTML such as React.
URLで与えたHTMLを取得してMarkdownに変換します。Reactなどの動的に生成されるHTMLには対応していません
""", inputs=[ gr.Text(label="URL", placeholder="https://*****"), gr.CheckboxGroup( label="Ignore tags(無視するタグ)", choices=["a", "img", "noscript"], value=["a", "img"], ), ], outputs=[gr.TextArea(label="Markdown", show_copy_button=True)], allow_flagging="never", ) demo.launch(server_name="0.0.0.0")