Simplehtmldom simple html dom php

PHP HTML DOM парсер с jQuery подобными селекторами

Добрый день, уважаемые хабровчане. В данном посте речь пойдет о совместном проекте S. C. Chen и John Schlick под названием PHP Simple HTML DOM Parser (ссылки на sourceforge).

Идея проекта — создать инструмент позволяющий работать с html кодом используя jQuery подобные селекторы. Оригинальная идея принадлежит Jose Solorzano’s и реализована для php четвертой версии. Данный же проект является более усовершенствованной версией базирующейся на php5+.

В обзоре будут представлены краткие выдержки из официального мануала, а также пример реализации парсера для twitter. Справедливости ради, следует указать, что похожий пост уже присутствует на habrahabr, но на мой взгляд, содержит слишком малое количество информации. Кого заинтересовала данная тема, добро пожаловать под кат.

Получение html кода страницы
$html = file_get_html('http://habrahabr.ru/'); //работает и с https:// 

Товарищ Fedcomp дал полезный комментарий насчет file_get_contents и 404 ответа. Оригинальный скрипт при запросе к 404 странице не возвращает ничего. Чтобы исправить ситуацию, я добавил проверку на get_headers. Доработанный скрипт можно взять тут.

Читайте также:  Html elements that can have text
Поиск элемента по имени тега
foreach($html->find('img') as $element) < //выборка всех тегов img на странице echo $element->src . '
'; // построчный вывод содержания всех найденных атрибутов src >
Модификация html элементов
$html = str_get_html('
Hello
World
'); // читаем html код из строки (file_get_html() - из файла) $html->find('div', 1)->class = 'bar'; // присвоить элементу div с порядковым номером 1 класс "bar" $html->find('div[id=hello]', 0)->innertext = 'foo'; // записать в элемент div с текст foo echo $html; // выведет
foo
World
Получение текстового содержания элемента (plaintext)
echo file_get_html('http://habrahabr.ru/')->plaintext; 

Целью статьи не является предоставить исчерпывающую документацию по данному скрипту, подробное описание всех возможностей вы можете найти в официальном мануале, если у сообщества возникнет желание, я с удовольствием переведу весь мануал на русский язык, пока же приведу обещанный в начале статьи пример парсера для twitter.

Пример парсера сообщений из twitter
require_once 'simple_html_dom.php'; // библиотека для парсинга $username = 'habrahabr'; // Имя в twitter $maxpost = '5'; // к-во постов $html = file_get_html('https://twitter.com/' . $username); $i = '0'; foreach ($html->find('li.expanding-stream-item') as $article) < //выбираем все li сообщений $item['text'] = $article->find('p.js-tweet-text', 0)->innertext; // парсим текст сообщения в html формате $item['time'] = $article->find('small.time', 0)->innertext; // парсим время в html формате $articles[] = $item; // пишем в массив $i++; if ($i == $maxpost) break; // прерывание цикла > 
Вывод сообщений

 for ($j = 0; $j < $maxpost; $j++) < echo '
'; echo '

' . $articles[$j]['text'] . '

'; echo '

' . $articles[$j]['time'] . '

'; echo '
'; >

Благодарю за внимание. Надеюсь, получилось не очень тяжеловесно и легко для восприятия.

Похожие библиотеки

P.S.
Хаброжитель Groove подсказал что подобные материалы уже были
P.P.S.
Постараюсь в свободное время собрать все библиотеки и составить сводные данные по производительности и приятности использования.

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

This is a mirror of the Simple HTML DOM Parser at

License

simplehtmldom/simplehtmldom

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

This adds unicode support for collapsing and replacing whitespace.

Git stats

Files

Failed to load latest commit information.

README.md

PHP Simple HTML DOM Parser

simplehtmldom is a fast and reliable HTML DOM parser for PHP.

  • Purely PHP-based DOM parser (no XML extensions required).
  • Works with well-formed and broken HTML documents.
  • Loads webpages, local files and document strings.
  • Supports CSS selectors.

simplehtmldom requires PHP 5.6 or higher with ext-iconv enabled. Following extensions enable additional features of the parser:

  • ext-mbstring (recommended)
    Enables better detection for multi-byte documents.
  • ext-curl
    Enables cURL support for the class HtmlWeb .
  • ext-openssl (recommended when using cURL)
    Enables SSL support for cURL.

Download the latest release from SourceForge and extract the files in the vendor folder of your project.

composer require simplehtmldom/simplehtmldom
git clone git://git.code.sf.net/p/simplehtmldom/repository simplehtmldom 

Note: The GitHub repository serves as a mirror for the SourceForge project. We currently accept pull requests and issues only via SourceForge.

This example illustrates how to return the page title:

load('https://www.google.com/search?q=simplehtmldom'); // Returns the page title echo $html->find('title', 0)->plaintext . PHP_EOL; 
load('https://www.google.com/search?q=simplehtmldom'); // Returns the page title echo $html->find('title', 0)->plaintext . PHP_EOL; 

Find more examples in the installation folder under examples .

The documentation for this library is hosted at https://simplehtmldom.sourceforge.io/docs/

There are various ways for you to get involved with simplehtmldom. Here are a few:

  • Share this project with your friends (Twitter, Facebook, . you name it. ).
  • Report bugs (SourceForge).
  • Request features (SourceForge).
  • Discuss existing bugs, features and ideas.

If you want to contribute code to the project, please open a feature request and include your patch with the message.

The source code for simplehtmldom is licensed under the MIT license. For further information read the LICENSE file in the root directory (should be located next to this README file).

simplehtmldom is a purely PHP-based DOM parser that doesn’t rely on external libraries like libxml, SimpleXML or PHP DOM. Doing so provides better control over the parsing algorithm and a much simpler API that even novice users can learn to use in a short amount of time.

About

This is a mirror of the Simple HTML DOM Parser at

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

📜 Modern Simple HTML DOM Parser for PHP

License

voku/simple_html_dom

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

📜 Simple Html Dom Parser for PHP

A HTML DOM parser written in PHP — let you manipulate HTML in a very easy way! This is a fork of PHP Simple HTML DOM Parser project but instead of string manipulation we use DOMDocument and modern php classes like «Symfony CssSelector».

  • PHP 7.0+ & 8.0 Support
  • PHP-FIG Standard
  • Composer & PSR-4 support
  • PHPUnit testing via Travis CI
  • PHP-Quality testing via SensioLabsInsight
  • UTF-8 Support (more support via «voku/portable-utf8»)
  • Invalid HTML Support (partly . )
  • Find tags on an HTML page with selectors just like jQuery
  • Extract contents from HTML in a single line

Install via «composer require»

composer require voku/simple_html_dom composer require voku/portable-utf8 # if you need e.g. UTF-8 fixed output
use voku\helper\HtmlDomParser; require_once 'composer/autoload.php'; ... $dom = HtmlDomParser::str_get_html($str); // or $dom = HtmlDomParser::file_get_html($file); $element = $dom->findOne('#css-selector'); // "$element" === instance of "SimpleHtmlDomInterface" $elements = $dom->findMulti('.css-selector'); // "$elements" === instance of SimpleHtmlDomNodeInterface $elementOrFalse = $dom->findOneOrFalse('#css-selector'); // "$elementOrFalse" === instance of "SimpleHtmlDomInterface" or false $elementsOrFalse = $dom->findMultiOrFalse('.css-selector'); // "$elementsOrFalse" === instance of SimpleHtmlDomNodeInterface or false ...

For support and donations please visit Github | Issues | PayPal | Patreon.

For status updates and release announcements please visit Releases | Twitter | Patreon.

For professional support please contact me.

  • Thanks to GitHub (Microsoft) for hosting the code and a good infrastructure including Issues-Managment, etc.
  • Thanks to IntelliJ as they make the best IDEs for PHP and they gave me an open source license for PhpStorm!
  • Thanks to Travis CI for being the most awesome, easiest continous integration tool out there!
  • Thanks to StyleCI for the simple but powerfull code style check.
  • Thanks to PHPStan && Psalm for relly great Static analysis tools and for discover bugs in the code!

Источник

Quick Start

Find below sample code that demonstrate the fundamental features of PHP Simple HTML DOM Parser.

Read plain text from HTML document

echo file_get_html('https://www.google.com/')->plaintext; 

Loads the specified HTML document into memory, parses it and returns the plain text. Note that file_get_html supports local files as well as remote files!

Read plaint text from HTML string

Parses the provided HTML string and returns the plain text. Note that the parser handles partial documents as well as full documents.

Read specific elements from HTML document

$html = file_get_html('https://www.google.com/'); foreach($html->find('img') as $element) echo $element->src . '
'; foreach($html->find('a') as $element) echo $element->href . '
';

Loads the specified document into memory and returns a list of image sources as well as anchor links. Note that find supports CSS selectors to find elements in the DOM.

Modify HTML documents

$doc = ' find('div', 1)->class = 'bar'; $html->find('div[id=hello]', 0)->innertext = 'foo'; echo $html; // Parses the provided HTML string and replaces elements in the DOM before returning the updated HTML string. In this example, the class for the second div element is set to bar and the inner text for the first div element to foo .

Note that find supports a second parameter to return a single element from the array of matches.

Note that attributes can be accessed directly by the means of magic methods ( ->class and ->innertext in the example above).

Collect information from Slashdot

$html = file_get_html('https://slashdot.org/'); $articles = $html->find('article[data-fhtype="story"]'); foreach($articles as $article) < $item['title'] = $article->find('.story-title', 0)->plaintext; $item['intro'] = $article->find('.p', 0)->plaintext; $item['details'] = $article->find('.details', 0)->plaintext; $items[] = $item; > print_r($items); 

Collects information from Slashdot for further processing.

Note that the combination of CSS selectors and magic methods make the process of parsing HTML documents a simple task that is easy to understand.

Источник

Оцените статью