# Gliederung Extrahieren
Auch wenn Sie nur auf die Ăberschriften zugreifen möchten, um z.B. die Anzahl oder LĂ€nge der Ăberschriften zu verarbeiten, reicht dies nicht immer aus. In einigen FĂ€llen mĂŒssen Sie vielleicht die tatsĂ€chliche Struktur des Inhalts ermitteln. FĂŒr diese AnwendungsfĂ€lle sollten Sie eine der folgenden Methoden in Betracht ziehen:
outlinefunktioniert Ă€hnlich wie die zuvor erwĂ€hnte Methodeheadings. Sie gibt ebenfalls alle Ăberschriften zurĂŒck, behĂ€lt aber die Struktur des Originaldokuments bei und liefert nur die Ăberschriftenebenen (z.B.h1) mit der Ausgabe.Die Methode
outlineWithParagraphsfunktioniert Àhnlich wieoutline, mit dem Unterschied, dass dieser Aufruf auch die AbsÀtze enthÀlt.cleanOutlineWithParagraphsfunktioniert Àhnlich wieoutlineWithParagraphs, mit dem Unterschied, dass alle leeren HTML-Tags entfernt werden.
Die folgenden Beispiele sollen helfen, die FunktionalitĂ€t besser zu verstehen. Es sind spezielle Methoden fĂŒr die SchlĂŒsselwort-Extraktion verfĂŒgbar.
# Extrahieren der Gliederung
Die Gliederung des Inhalts ermöglicht es Ihnen, einen Index des Dokuments zu erstellen. Im folgenden Beispiel wird eine Markdown-Version der Ăberschriften des angeforderten Dokuments erstellt:
$web = new \Spekulatius\PHPScraper\PHPScraper;
/**
* Navigation zur Testseite. Diese Seite enthÀlt:
*
* <h1>We are testing here!</h1>
* [...]
*
* <h2>Examples</h2>
* [...]
*
* <h3>Example 1</h3>
* [...]
*
* <h3>Example 2</h3>
* [...]
*
* <h3>Example 3</h3>
* [...]
*/
$web->go('https://test-pages.phpscraper.de/content/outline.html');
/**
* $outline wird gesetzt auf:
*
* [
* [
* "tag" => "h1",
* "content" => "We are testing here!"
* ], [
* "tag" => "h2",
* "content" => "Examples"
* ], [
* "tag" => "h3",
* "content" => "Example 1"
* ], [
* "tag" => "h3",
* "content" => "Example 2"
* ], [
* "tag" => "h3",
* "content" => "Example 3"
* ]
* ]
*/
$outline = $web->outline;
# Gliederung mit AbsÀtzen extrahieren
Die folgende Methode funktioniert Ă€hnlich wie outline, aber sie schlieĂt auch alle AbsĂ€tze als Teil des zurĂŒckgegebenen Arrays ein:
$web = new \Spekulatius\PHPScraper\PHPScraper;
/**
* Navigation zur Testseite. Diese Seite enthÀlt:
*
* <h1>We are testing here!</h1>
* <p>This page contains an example structure to be parsed. It comes with a number of headings and nested paragraphs as an scrape example.</p>
*
* <h2>Examples</h2>
* <p>There are numerous examples on the website. Please check them out to get more context on how scraping works.</p>
*
* <h3>Example 1</h3>
* <p>Here would be an example.</p>
*
* <h3>Example 2</h3>
* <p>Here would be the second example.</p>
*
* <h3>Example 3</h3>
* <p>Here would be another example.</p>
*
* <!-- an empty paragraph to check if it gets filtered out correctly -->
* <p></p>
*/
$web->go('https://test-pages.phpscraper.de/content/outline.html');
$content = $web->outlineWithParagraphs;
/**
* $content now contains:
*
* [
* [
* "tag" => "h1",
* "content" => "We are testing here!"
* ], [
* "tag" => "p",
* "content" => "This page contains an example structure to be parsed. It comes with a number of headings and nested paragraphs as an scrape example."
* ], [
* "tag" => "h2",
* "content" => "Examples"
* ], [
* "tag" => "p",
* "content" => "There are numerous examples on the website. Please check them out to get more context on how scraping works."
* ], [
* "tag" => "h3",
* "content" => "Example 1"
* ], [
* "tag" => "p",
* "content" => "Here would be an example."
* ], [
* "tag" => "h3",
* "content" => "Example 2"
* ], [
* "tag" => "p",
* "content" => "Here would be the second example."
* ], [
* "tag" => "h3",
* "content" => "Example 3"
* ], [
* "tag" => "p",
* "content" => "Here would be another example."
* ], [
* "tag" => "p",
* "content" => ""
* ]
* ]
*/
# Extrahieren der bereinigten Gliederung mit AbsÀtzen
Die folgende Methode funktioniert Ă€hnlich wie outlineWithParagraphs, aber diese enthĂ€lt keine leeren Ăberschriften oder AbsĂ€tze als Teil des zurĂŒckgegebenen Arrays:
$web = new \Spekulatius\PHPScraper\PHPScraper;
/**
* Navigation zur Testseite. Diese Seite enthÀlt:
*
* <h1>We are testing here!</h1>
* <p>This page contains an example structure to be parsed. It comes with a number of headings and nested paragraphs as an scrape example.</p>
*
* <h2>Examples</h2>
* <p>There are numerous examples on the website. Please check them out to get more context on how scraping works.</p>
*
* <h3>Example 1</h3>
* <p>Here would be an example.</p>
*
* <h3>Example 2</h3>
* <p>Here would be the second example.</p>
*
* <h3>Example 3</h3>
* <p>Here would be another example.</p>
*
* <!-- an empty paragraph to check if it gets filtered out correctly -->
* <p></p>
*/
$web->go('https://test-pages.phpscraper.de/content/outline.html');
$content = $web->cleanOutlineWithParagraphs;
/**
* $content enthÀlt jetzt:
*
* [
* [
* "tag" => "h1",
* "content" => "We are testing here!"
* ], [
* "tag" => "p",
* "content" => "This page contains an example structure to be parsed. It comes with a number of headings and nested paragraphs as an scrape example."
* ], [
* "tag" => "h2",
* "content" => "Examples"
* ], [
* "tag" => "p",
* "content" => "There are numerous examples on the website. Please check them out to get more context on how scraping works."
* ], [
* "tag" => "h3",
* "content" => "Example 1"
* ], [
* "tag" => "p",
* "content" => "Here would be an example."
* ], [
* "tag" => "h3",
* "content" => "Example 2"
* ], [
* "tag" => "p",
* "content" => "Here would be the second example."
* ], [
* "tag" => "h3",
* "content" => "Example 3"
* ], [
* "tag" => "p",
* "content" => "Here would be another example."
* ]
* ]
*/