Cool HTML Parser

Top 10 Best Usage Examples of PHP Simple HTML DOM Parser

Simple HTML DOM Parser is one of the best things that has happened to me. I remember the days when I used to use regular expressions and preg_match_all function to fetch values from scraped text, they were not so good. But ever since I found this HTML DOM Parser, life has been way too easy when it comes to fetching data and extracting values from html pages.

During my initial days while using this script, I was confused quite a lot of times. The parser is actually so awesome that it provides too many features and it can do almost everything you would want a parser to do. Only problem is to remember the syntax and method of calling various functions along with numerous distinct parameters for each of them.

I’ve made a list of codes, which I use from time to time, that can come in handy for you all. Read further to understand the usage of Simple HTML DOM Parser and get readymade PHP codes for the same.

Читайте также:  Генерация словарей python 3

Downloading and storing structured data

Data can be obtained from mainly three different sources : URL, Static File or HTML String. Use the following code to create a DOM from three different alternatives.

PHP Simple HTML DOM Parser

PHP Simple HTML DOM Parser is the best HTML DOM parser in any programming language.

"); //to fetch a webpage in a string and then parse $data = file_get_contents("http://nimishprabhu.com"); //or you can use curl too, like me :) // Some manipulation with the $data variable, for e.g. $data = str_replace("Nimish", "NIMISH", $data); //now parsing it into html $html = str_get_html($data); ?>

Finding HTML elements based on their tag names

Suppose you wanted to find each and every image on a webpage or say, each and every hyperlink. We will be using “find” function to extract this information from the object. Here’s how to do it using Simple HTML DOM Parser :

find('a') as $a) < $links[] = $a->href; > print_r($links); //to fetch all images from a webpage $images = array(); foreach($html->find('img') as $img) < $images[] = $img->src; > print_r($images); //to find h1 headers from a webpage $headlines = array(); foreach($html->find('h1') as $header) < $headlines[] = $header->plaintext; > print_r($headlines); ?>

Extracting values of attributes from elements

Suppose you want to get names of all input fields on a webpage, let’s say for e.g., http://nimishprabhu.com/chrome-extension-hello-world-example.html. Now if you see the webpage you will notice that there is a comment form on the page which has input fields. Please note that the comment box is a textarea element and not input element, so it will not be detected. But to detect rest of the visible as well has hidden fields you can use following code :

find('input') as $input) < echo $input->name.'
'; > // Output for above script : // author // email // url // submit // comment_post_ID // comment_parent ?>

Filtering elements based on values of its attributes

When a developer designs a page, he uses various attributes to uniquely identify and classify the information on the webpage. A parser is not human and hence cannot visualize the difference, but it can detect these attributes and filter the output so as to obtain a precise set of data. Let us take a practical example for better understanding. If you see this page : https://www.phpbb.com/community/viewtopic.php?f=46&t=543171 you can see the page is divided into header, content and footer. Now even the content is further sub divided into posts. This page has only 1 post but I decided to choose this as it contains quite a lot of hyperlinks. Now suppose you wanted to extract only the hyperlinks in the post and not the entire page. The approach should be as follows : Check the source of the webpage. Find out whether the hyperlinks are following some kind of pattern. If you look closely you will find that all of them have This will make extracting them, a piece of cake. Read the code below to see how to filter html elements based on values of attributes.

find('a[class="postlink"]') as $a) < $links[] = $a->href; > print_r($links); ?>

There is something worth noting here, you can use “.” and “#” prefixes to filter class and id attributes respectively. So the above code will work without any change if you use the filter as :

foreach($html->find('a.postlink') as $a)

Pattern matching while filtering attributes of elements

Consider the above example where we are extracting all links from the post. Say you want to find only the links of the sub forums in the community. If you notice all of them begin with http://www.phpbb.com/community/viewforum.php. So let’s filter the hyperlinks using “starts with” filter to fetch only the links starting with http://www.phpbb.com/community/viewforum.php

find('a[href^="http://www.phpbb.com/community/viewforum.php"]') as $a) < $links[] = $a->href; > print_r($links); ?>

Similarly, say if you want to find all links containing phpbb.com then you can filter using “contains” filter as follows :

foreach($html->find('a[href*="phpbb.com"]') as $a)

If you are sure about only the end part of the value of an attribute. Let’s say, for e.g., you are scrapping a webpage which contains numerous div elements. These div elements have the id attribute something like :

and so on.
Then you can find such div elements using the “ends with” filter as follows :

foreach($html->find('div[id$="_message_id"]' as $div)

Adding / Changing attributes of the elements

Let’s say you want to change the value of attribute of particular element. For e.g. if you wished to change all the hyperlinks having to you can do so as follows :

find('a.postlink') as $a) < $a->class = 'topiclink'; > echo $html; ?>

Finding nth element from parsed data

Note that the numbering of elements starts from 0 and not 1. Thus the first element will be found at 0th location. Let’s assume that you want to extract the hyperlink of the 3rd link with class postlink on a webpage, you can use the following approach :

Manipulating the inner content of tags

$html->find('div#content',0)->innertext = '';
$appendcode = '

This is the text to append to existing innertext

'; $html->find('div#content',0)->innertext .= $appendcode;
$prependcode = '

Nice article below

'; $html->find('div#content,0)->innertext = $prependcode . $html->find('div#content',0)->innertext;

Wrap the contents of an element inside a new element

Say you have an existing div with id content, now you made a wrapper div and want to enclose the content div in the wrapper div. Here’s how you do it :

$html->find('div#content',0)->outertext = '
find('div#content',0)->outertext. '
';

Handling memory leak issues while using PHP Simple HTML DOM Parser

Last but definitely not the least, handling the memory leak issue. Once you start using this script extensively you will encounter memory exhausted errors and will keep wondering what’s wrong with your script. The problem might be due to not handling the memory leak issue. I will not talk in detail about what memory leak is or how this issue is caused but you can read quite a bit about it here.To handle this issue don’t forget to clear the $html variable created and unset it once it’s not required further.

I guess these examples are sufficient enough for you to get started with using PHP Simple HTML DOM Parser. If you have any doubts or queries use the comment form below. I will add more examples as per requests and queries. Hope this article helps you scrape data efficiently.

Источник

Как в PHP находить ссылки без регулярных выражений

От автора: не люблю каждый раз натыкаться на одни и те же грабли! Вот сегодня опять та тема, в которой никак не обойтись без регулярных выражений. Это и есть мои любимые «грабли». Но все равно я не сдамся, и чтобы с помощью PHP находить ссылки, я обойдусь без них!

Никуда без них не деться!

Нет уж, господа консерваторы! Я постараюсь уж как-нибудь реализовать парсинг документов без этого застарелого средства. Ну не хватает у меня терпения на составление шаблонов с помощью регулярных выражений. А когда терпение лопается, то рождаются другие более «ругательные» выражения :). Так что «грабли» в сторону – мы идем по собственному галсу!

Чтобы не опростоволоситься, нам потребуется сторонняя библиотека — Simple HTML DOM. Скачать ее можно по этой ссылке. Не беспокойтесь, версия хоть и старая, но работает. А главное, что это средство посвежее будет, чем выражения регулярные :).

Как в PHP находить ссылки без регулярных выражений

После распаковки помещаем файл simple_html_dom.php в папку со скриптом, чтоб легче было подключать. Все остальные файлы в принципе нас не интересуют, но пригодятся вам в будущем. Там есть и мануал, и примеры использования библиотеки.

Онлайн курс «PHP-разработчик»

Изучите курс и создайте полноценный проект — облачное хранилище файлов

С нуля освоите язык программирования PHP, структурируете имеющиеся знания, а эксперты помогут разобраться с трудными для понимания темами, попрактикуетесь на реальных задачах. Напишете первый проект для портфолио.

Как в PHP находить ссылки без регулярных выражений

Реализуем!

Напомню, что сегодня мы научимся, как найти ссылки PHP без «ужасных» регулярных выражений. Теперь нам осталось подключить скрипт библиотеки у себя в коде и просканировать указанную веб-страницу на наличие гиперссылок.

Источник

PHP Simple HTML DOM Parser, парсинг ссылки

Вам надо, разумеется, echo $a->href; Ссылка равна # , очевидно. Вы уверены, что вам надо именно это? Небось настоящая ссылка выставляется джаваскриптом на onclick .

2 ответа 2

Намного проще воспользоваться встроенным классом.
Идея: ищем ссылку с нужным классом -> удаляем все вложенные элементы. После этого останется только внутренний текст, который не принадлежит внутренним элементам.
Демонстрация: http://ideone.com/nhFd5p
Код:

МВО 
МВО - масловодоотталкивающая отделка

Благодаря масловодоотталкивающей отделке ткань препятствует проникновению воды и жировых, масляных жидкостей, сохраняя при этом воздухо- (паро- ) проницаемость ( ткань пропускает пары пота). Тестирование проводится нанесением тестирующих жидкостей по методике ф. "3М".

'; $output = array(); $doc = new DomDocument(); $doc->loadHTML('' . $html); /* Корректно обрабатываем UTF-8 */ // dirty fix foreach ($doc->childNodes as $item) if ($item->nodeType == XML_PI_NODE) $doc->removeChild($item); // remove hack $doc->encoding = 'UTF-8'; // insert proper $urls = $doc->getElementsByTagName('a'); foreach ($urls as $url) < if ($url->getAttribute('class') === 'seq') < $childs = $url->getElementsByTagName('*'); foreach ($childs as $child) < $url->removeChild($child); > $output[] = trim($url->nodeValue); > > print_r($output);

Источник

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