Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
---
category: general
date: 2026-09-26
description: تحويل HTML إلى Markdown باستخدام Python، استخراج الروابط من HTML وحفظ
HTML كـ Markdown. تعلم كيفية تحويل HTML خطوة بخطوة.
draft: false
images:
- PLACEHOLDER_URL/og-image.png
keywords:
- convert html to markdown
- extract links from html
- save html as markdown
- how to convert html
- extract paragraphs from html
language: ar
lastmod: 2026-09-26
og_description: حوّل HTML إلى Markdown باستخدام Python، استخراج الروابط من HTML وحفظ
HTML كـ Markdown. اتبع هذا الدليل الكامل.
og_image_alt: Screenshot of Python code converting HTML to Markdown and showing extracted
links
og_title: تحويل HTML إلى Markdown في بايثون – استخراج الروابط والفقرات
schemas:
- author: GroupDocs
dateModified: '2026-09-26'
description: Convert HTML to Markdown with Python, extracting links from HTML and
saving HTML as Markdown. Learn how to convert HTML step‑by‑step.
headline: Convert HTML to Markdown in Python – extract links and paragraphs easily
type: TechArticle
- description: Convert HTML to Markdown with Python, extracting links from HTML and
saving HTML as Markdown. Learn how to convert HTML step‑by‑step.
name: Convert HTML to Markdown in Python – extract links and paragraphs easily
steps:
- name: Expected output
text: 'Running the script generates a file similar to the following (the exact
content depends on the source HTML):'
- name: 1. Extract only links
text: '```python md_options.features = MarkdownFeatures.LINKS # No paragraphs
```'
- name: 2. Extract only paragraphs
text: '```python md_options.features = MarkdownFeatures.PARAGRAPHS # No links
```'
type: HowTo
- questions:
- answer: Yes. `HTMLDocument` accepts any well‑formed fragment; the converter treats
the fragment as the document body.
question: Does this work with HTML fragments (no `<html>` root tag)?
- answer: 'Add `MarkdownFeatures.IMAGES` to the `features` flag: ```python md_options.features
= MarkdownFeatures.LINKS | MarkdownFeatures.PARAGRAPHS | MarkdownFeatures.IMAGES
```'
question: Can I keep images as Markdown image syntax?
- answer: 'Wrap `convert_html_to_markdown` in a loop that walks the directory with
`os.listdir` or `pathlib.Path.rglob("*.html")`. --- ## Conclusion You now know
how to **convert HTML to Markdown** in Python while selectively **extracting
links from HTML** and **extracting paragraphs from HTML**. The script de'
question: How do I convert many files in a directory?
type: FAQPage
tags:
- html
- markdown
- python
- data‑extraction
title: تحويل HTML إلى Markdown في بايثون – استخراج الروابط والفقرات بسهولة
url: /ar/python/general/convert-html-to-markdown-in-python-extract-links-and-paragra/
---

{{< blocks/products/pf/main-wrap-class >}}
{{< blocks/products/pf/main-container >}}
{{< blocks/products/pf/tutorial-page-section >}}

# تحويل HTML إلى Markdown في بايثون – استخراج الروابط والفقرات بسهولة

إذا كنت بحاجة إلى **تحويل HTML إلى Markdown** مع الاحتفاظ بالأجزاء المفيدة فقط، يوضح لك هذا الدليل كيفية القيام بذلك ببضع أسطر من بايثون. سواءً كنت تقوم بجمع مشاركات المدونات، أو أرشفة الوثائق، أو تنظيف محتوى رسائل البريد الإلكتروني، ستتعلم طريقة موثوقة لاستخراج الروابط من HTML وحفظ HTML كـ Markdown.

يغطي الدليل كل شيء من تثبيت الحزمة المطلوبة إلى معالجة الحالات الخاصة مثل وسوم `<a>` الفارغة أو الفقرات المتداخلة. في النهاية ستحصل على سكريبت جاهز للتنفيذ **يحول HTML إلى Markdown**، ويستخرج الروابط من HTML، وحتى يستخرج الفقرات من HTML عندما تحتاجها.

---

## المتطلبات المسبقة

قبل أن تبدأ، تأكد من أن لديك:

* Python 3.8 أو أحدث مثبتًا
* إمكانية الوصول إلى حزمة بايثون `groupdocs-conversion` (المكتبة التي توفر `HTMLDocument` و `MarkdownSaveOptions` و `Converter`)
* ملف HTML محلي تريد معالجته (مثال: `article.html`)

يمكنك تثبيت المكتبة باستخدام pip:

```bash
pip install groupdocs-conversion
```

> **نصيحة احترافية:** استخدم بيئة افتراضية (`python -m venv venv`) لعزل الاعتمادات.

---

## الخطوة 1: تحميل مستند HTML المصدر

العملية الأولى هي إنشاء كائن `HTMLDocument` يشير إلى ملف المصدر الخاص بك. هذا الكائن يج abstracts الـ HTML الخام ويعطي المحول نقطة دخول نظيفة.

```python
from groupdocs.conversion import HTMLDocument

# Load the HTML file you want to transform
html_doc = HTMLDocument("YOUR_DIRECTORY/article.html")
```

*لماذا هذا مهم:* تحميل المستند بهذه الطريقة يسمح للمكتبة بتحليل DOM مرة واحدة، بحيث تكون العمليات اللاحقة (مثل استخراج الروابط أو الفقرات) سريعة وفعّالة في الذاكرة.

---

## الخطوة 2: إنشاء خيارات حفظ Markdown واختيار الميزات التي تحتاجها

`MarkdownSaveOptions` يتيح لك تحديد أي عناصر HTML ستبقى بعد التحويل. علم `features` يستخدم عملية OR بتية لدمج الخيارات.

```python
from groupdocs.conversion import MarkdownSaveOptions, MarkdownFeatures

# Keep only links and paragraphs in the resulting Markdown
md_options = MarkdownSaveOptions()
md_options.features = MarkdownFeatures.LINKS | MarkdownFeatures.PARAGRAPHS
```

*لماذا هذا مهم:* بتحديد `LINKS` و `PARAGRAPHS` تقوم **باستخراج الروابط من HTML** و **باستخراج الفقرات من HTML** مع تجاهل كل شيء آخر (الأنماط، السكريبتات، الصور). إذا احتجت لاحقًا الروابط فقط، استبدل `MarkdownFeatures.PARAGRAPHS` بـ `0` (أو احذفها).

---

## الخطوة 3: تحويل HTML إلى Markdown باستخدام الخيارات المكوّنة

الآن استدعِ الطريقة الساكنة `convert_html`، مع تمرير مستند المصدر، مسار الوجهة، والخيارات التي أنشأتها للتو.

```python
from groupdocs.conversion import Converter

# Perform the conversion and write the Markdown file
Converter.convert_html(html_doc, "YOUR_DIRECTORY/article_links.md", md_options)
```

*لماذا هذا مهم:* التحويل يتم في مرور واحد، مطبقًا مرشح الميزات الذي حددته. الملف الناتج (`article_links.md`) يحتوي فقط على روابط وفقرات بصيغة Markdown، وهو بالضبط ما تحتاجه عندما تريد **حفظ HTML كـ Markdown** للمعالجة اللاحقة.

---

## البرنامج الكامل – كل شيء معًا

فيما يلي سكريبت كامل قابل للتنفيذ يمكنك نسخه‑لصقه في ملف اسمه `html_to_md.py`. عدّل المسارات لتتناسب مع بيئتك.

```python
# html_to_md.py
# -------------------------------------------------
# Convert HTML to Markdown, keeping only links and paragraphs.
# -------------------------------------------------

from groupdocs.conversion import HTMLDocument, MarkdownSaveOptions, MarkdownFeatures, Converter

def convert_html_to_markdown(source_path: str, target_path: str) -> None:
"""
Convert an HTML file to Markdown, extracting only links and paragraphs.

Args:
source_path: Path to the source HTML file.
target_path: Path where the Markdown file will be saved.
"""
# Load the HTML document
html_doc = HTMLDocument(source_path)

# Configure conversion to keep links and paragraphs
md_options = MarkdownSaveOptions()
md_options.features = MarkdownFeatures.LINKS | MarkdownFeatures.PARAGRAPHS

# Run the conversion
Converter.convert_html(html_doc, target_path, md_options)
print(f"✅ Conversion complete – Markdown saved to: {target_path}")

if __name__ == "__main__":
# Example usage – replace with your actual file locations
src = "YOUR_DIRECTORY/article.html"
dst = "YOUR_DIRECTORY/article_links.md"
convert_html_to_markdown(src, dst)
```

### النتيجة المتوقعة

تشغيل السكريبت يولد ملفًا مشابهًا للآتي (المحتوى الدقيق يعتمد على HTML المصدر):

```markdown
[OpenAI](https://openai.com)

This is the first paragraph of the article.

[GitHub](https://github.com)

Another paragraph that explains the next topic.
```

فقط نص الرابط ونص الفقرة يظهران؛ جميع عناصر HTML الأخرى تُزال.

---

## استخراج الروابط فقط أو الفقرات فقط (تغيّرات متقدمة)

أحيانًا تحتاج **كيفية تحويل HTML** إلى ملف Markdown يحتوي على نوع واحد فقط من العناصر.

### 1. استخراج الروابط فقط

```python
md_options.features = MarkdownFeatures.LINKS # No paragraphs
```

### 2. استخراج الفقرات فقط

```python
md_options.features = MarkdownFeatures.PARAGRAPHS # No links
```

كلا التغيّرين يعيدان استخدام نفس استدعاء `convert_html`، لذا لا تحتاج إلى كتابة منطق تحويل منفصل.

---

## معالجة الحالات الخاصة

| الحالة | الحل المقترح |
|----------------------------------------|-----------------|
| ملف HTML يحتوي على وسوم `<a>` فارغة | المحول يتخطى الروابط الفارغة تلقائيًا. إذا رأيت إدخالات `[]()` غريبة، اضبط `md_options.removeEmptyLinks = True`. |
| فقرات متداخلة (`<p>` داخل `<div>`) | المكتبة تُسطّح الفقرات المتداخلة، محافظةً على ترتيب النص. لا حاجة لكود إضافي. |
| حروف غير ASCII في عناوين الروابط | تأكد من حفظ ملف بايثون بترميز UTF‑8 وافتح ملف الإخراج باستخدام `encoding="utf-8"` إذا قرأته لاحقًا. |
| ملفات HTML كبيرة جدًا (≥ 50 ميغابايت) | عالج الملف على دفعات باستخدام `HTMLDocument(stream=io.BytesIO(...))` لتجنب تحميل الملف بالكامل في الذاكرة. |

---

## الأسئلة المتكررة

**س: هل يعمل هذا مع أجزاء HTML (بدون وسم `<html>` الجذري)؟**
ج: نعم. `HTMLDocument` يقبل أي جزء مُشكل جيدًا؛ المعالج يتعامل مع الجزء كجسم المستند.

**س: هل يمكنني الاحتفاظ بالصور بصيغة صورة Markdown؟**
ج: أضف `MarkdownFeatures.IMAGES` إلى علم `features`:
```python
md_options.features = MarkdownFeatures.LINKS | MarkdownFeatures.PARAGRAPHS | MarkdownFeatures.IMAGES
```

**س: كيف يمكنني تحويل العديد من الملفات في دليل؟**
ج: غلف `convert_html_to_markdown` في حلقة تتجول في الدليل باستخدام `os.listdir` أو `pathlib.Path.rglob("*.html")`.

---

## الخلاصة

أنت الآن تعرف كيف **تحول HTML إلى Markdown** في بايثون مع استخراج الروابط والفقرات من HTML بشكل انتقائي. يوضح السكريبت النهج القياسي—تحميل المستند، تكوين `MarkdownSaveOptions`، ثم تشغيل `Converter.convert_html`. مع بعض التعديلات يمكنك أيضًا **حفظ HTML كـ Markdown** يحتوي فقط على الروابط، أو الفقرات فقط، أو تمثيل كامل ودقيق.

بعد ذلك، قد ترغب في استكشاف:

* إضافة `MarkdownFeatures.HEADINGS` للحفاظ على عناوين الأقسام.
* استخدام الـ Markdown الناتج كمدخل لمولدات مواقع ثابتة مثل MkDocs أو Hugo.
* أتمتة التحويلات الجماعية لمستودع وثائق كامل.

تحويل ممتع!

## ما الذي يجب أن تتعلمه بعد ذلك؟

الدروس التالية تغطي مواضيع ذات صلة وثيقة تبني على التقنيات الموضحة في هذا الدليل. كل مورد يتضمن أمثلة شفرة كاملة مع شروحات خطوة‑بخطوة لمساعدتك على إتقان ميزات API إضافية واستكشاف أساليب تنفيذ بديلة في مشاريعك.

- [تحويل HTML إلى Markdown في .NET باستخدام Aspose.HTML](/html/english/net/html-extensions-and-conversions/convert-html-to-markdown/)
- [تحويل HTML إلى Markdown في Aspose.HTML للـ Java](/html/english/java/saving-html-documents/convert-html-to-markdown/)
- [كيفية ضبط الإزاحة عند تحويل HTML إلى Markdown في Java](/html/english/java/conversion-html-to-other-formats/how-to-set-offset-when-converting-html-to-markdown-in-java/)

{{< /blocks/products/pf/tutorial-page-section >}}
{{< /blocks/products/pf/main-container >}}
{{< /blocks/products/pf/main-wrap-class >}}
{{< blocks/products/products-backtop-button >}}
Loading
Loading