Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ llm-web-kit is a python library that ..

## Quick Start

### extract magic_html+recognize
### extract by magic_html+recognize

```python
from llm_web_kit.simple import extract_html_to_md
from llm_web_kit.simple import extract_html_to_md, extract_html_to_mm_md
import traceback
from loguru import logger

Expand All @@ -100,13 +100,14 @@ if __name__=="__main__":
### only extract by recognize

```python
from llm_web_kit.simple import extract_pure_html_to_md
from llm_web_kit.simple import extract_html_to_md, extract_html_to_mm_md
import traceback
from loguru import logger

def extract(url:str, main_html:str, raw_html) -> str:
def extract(url:str, raw_html:str) -> str:
try:
nlp_md = extract_html_to_md(url, main_html, clip_html=False, raw_html)
nlp_md = extract_html_to_md(url, raw_html, clip_html=False)
# or mm_nlp_md = extract_html_to_mm_md(url, raw_html, clip_html=False)
return nlp_md
except Exception as e:
logger.exception(e)
Expand All @@ -121,7 +122,7 @@ if __name__=="__main__":
### only extract main_html by magic-html

```python
from llm_web_kit.simple import extract_magic_html
from llm_web_kit.simple import extract_main_html_by_maigic_html
import traceback
from loguru import logger

Expand Down
9 changes: 8 additions & 1 deletion llm_web_kit/extractor/html/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,14 @@ def _do_extract(self, data_json: DataJson) -> DataJson:
self._extract_image,
self._extract_title, self._extract_paragraph]:
parsed_html = extract_func(base_url, parsed_html, raw_html)
content_list:ContentList = self._export_to_content_list(base_url, parsed_html, raw_html)

# 过滤掉包含script和style标签的元素
filtered_parsed_html = []
for cc_html, o_html in parsed_html:
# 检查o_html是否包含script或style标签
if not (o_html.xpath('//script') or o_html.xpath('//style')):
filtered_parsed_html.append((cc_html, o_html))
content_list:ContentList = self._export_to_content_list(base_url, filtered_parsed_html, raw_html)
data_json['content_list'] = content_list
# data_json['title'] = title
return data_json
Expand Down
111 changes: 111 additions & 0 deletions tests/llm_web_kit/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,113 @@ class TestSimple(unittest.TestCase):
def setUp(self):
self.url = 'https://example.com'
self.html_content = '<html><body><h1>Test Content</h1><p>This is a test paragraph.</p><img src="https://example.com/image.jpg" alt="Test Image" /></body></html>'
self.real_html_content = """
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML标签综合示例</title>

<!-- 内嵌CSS样式 -->
<style>
/* 基础样式设置 */
body {
font-family: 'Microsoft YaHei', sans-serif;
line-height: 1.6;
margin: 0;
padding: 0;
background-color: #f5f5f5;
}

/* 页眉样式 */
header {
background-color: #2c3e50;
color: white;
padding: 1rem;
text-align: center;
}

/* 主体内容样式 */
main {
max-width: 800px;
margin: 2rem auto;
padding: 1rem;
background: white;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}

/* 交互按钮样式 */
.action-btn {
background-color: #3498db;
color: white;
border: none;
padding: 0.8rem 1.5rem;
cursor: pointer;
transition: background-color 0.3s;
}

.action-btn:hover {
background-color: #2980b9;
}
</style>
</head>
<body>
<!-- 语义化页眉 -->
<header>
<h1>网页标题</h1>
<nav>
<ul style="list-style-type: none; display: flex; justify-content: center; gap: 1rem;">
<li><a href="#" style="color: white; text-decoration: none;">首页</a></li>
<li><a href="#" style="color: white; text-decoration: none;">关于</a></li>
</ul>
</nav>
</header>

<!-- 主体内容区域 -->
<main>
<article>
<h2>文章标题</h2>
<p>这是有效的文本内容,展示了如何在HTML文档中组织实际内容。</p>

<!-- 带交互的元素 -->
<button id="demoBtn" class="action-btn">点击演示</button>
<div id="dynamicContent" style="margin-top: 1rem; display: none;">
<p>这是通过JavaScript动态显示的内容!</p>
</div>
</article>
</main>

<!-- 页脚区域 -->
<footer style="text-align: center; padding: 1rem; background-color: #34495e; color: white;">
<p>© 2025 版权所有</p>
</footer>

<!-- 内嵌JavaScript -->
<script>
// 等待DOM加载完成
document.addEventListener('DOMContentLoaded', function() {
const demoBtn = document.getElementById('demoBtn');
const dynamicContent = document.getElementById('dynamicContent');

// 按钮点击事件处理
demoBtn.addEventListener('click', function() {
dynamicContent.style.display = 'block';
this.textContent = '已显示内容';

// 创建动态元素
const newElement = document.createElement('p');
newElement.textContent = '这是动态创建的元素!';
newElement.style.color = '#e74c3c';
dynamicContent.appendChild(newElement);
});

console.log('页面初始化完成');
});
</script>
</body>
</html>
"""

def test_extractor_factory(self):
# Setup mocks
Expand All @@ -30,3 +137,7 @@ def test_extract_pure_html_to_mm_md(self):
def test_extract_magic_html(self):
magic_html, title = extract_main_html_by_maigic_html(self.url, self.html_content)
self.assertEqual(magic_html, '<div><body><h1>Test Content</h1><p>This is a test paragraph.</p><img src="https://example.com/image.jpg" alt="Test Image"></body></div>')

def test_extract_real_html_to_md(self):
md = extract_html_to_md(self.url, self.real_html_content, clip_html=False)
assert 'DOMContentLoaded' not in md