wechat-article-parser 0.0.1__tar.gz → 0.0.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -21,6 +21,7 @@ venv/
21
21
 
22
22
  # Claude Code
23
23
  .claude/
24
+ CLAUDE.md
24
25
 
25
26
  # OS
26
27
  .DS_Store
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: wechat-article-parser
3
- Version: 0.0.1
3
+ Version: 0.0.2
4
4
  Summary: WeChat MP article parser - extract metadata and content from WeChat public account articles
5
5
  License-Expression: MIT
6
6
  License-File: LICENSE
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "wechat-article-parser"
7
- version = "0.0.1"
7
+ version = "0.0.2"
8
8
  description = "WeChat MP article parser - extract metadata and content from WeChat public account articles"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -236,18 +236,37 @@ def _extract_rich_media_content(content_tag: Tag, result: ArticleResult) -> None
236
236
  tag.decompose()
237
237
 
238
238
  # 处理含有 background-image 的 <svg> 标签
239
- # 这些通常是装饰性元素(分隔线、底纹等),转为 <img> 用于 Markdown 渲染,但不计入 images 列表
239
+ # 部分文章用嵌套 SVG(外层 SVG > foreignobject > 内层 SVG)承载正文图片,
240
+ # 需从外向内收集所有图片 URL,一次性替换为多个 <img> 标签
240
241
  to_remove = []
241
- for svg in soup.find_all("svg"):
242
- style = svg.get("style", "")
243
- if "background-image" in style:
242
+ for svg in soup.find_all("svg", recursive=True):
243
+ # 跳过已被外层 SVG 处理过的嵌套 SVG(已脱离文档树)
244
+ if not svg.parent:
245
+ continue
246
+ # 跳过嵌套在其他 SVG 内的 SVG,由外层统一处理
247
+ if svg.find_parent("svg"):
248
+ continue
249
+
250
+ # 收集本 SVG 及所有后代 SVG 中的 background-image 图片
251
+ all_svgs = [svg] + svg.find_all("svg")
252
+ img_tags = []
253
+ for s in all_svgs:
254
+ style = s.get("style", "")
255
+ if "background-image" not in style:
256
+ continue
244
257
  m = re.search(r'url\("([^"]+)"\)', style)
245
- if m:
246
- normalized = _normalize_image_url(m.group(1))
247
- new_img = soup.new_tag("img", src=normalized)
248
- svg.replace_with(new_img)
258
+ if not m:
249
259
  continue
250
- to_remove.append(svg)
260
+ normalized = _normalize_image_url(m.group(1))
261
+ img_tags.append(soup.new_tag("img", src=normalized))
262
+ if normalized not in seen:
263
+ seen.add(normalized)
264
+ result.images.append(normalized)
265
+
266
+ if img_tags:
267
+ svg.replace_with(*img_tags)
268
+ else:
269
+ to_remove.append(svg)
251
270
  for tag in to_remove:
252
271
  tag.decompose()
253
272
 
@@ -11,6 +11,7 @@ TEST_URLS = [
11
11
  "https://mp.weixin.qq.com/s/0Wz3JeMbtWBL5iWJgYPS_Q",
12
12
  "https://mp.weixin.qq.com/s/DmZXjgIzq5gBo3H-YtcjVw",
13
13
  "https://mp.weixin.qq.com/s/80bysSCadvy9VbaovXBv2g",
14
+ "https://mp.weixin.qq.com/s/sfwGsafriO9sm6PfbQVXhw",
14
15
  ]
15
16
 
16
17