PHPackages                             stil/xpath-selector - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. stil/xpath-selector

AbandonedArchivedLibrary[Utility &amp; Helpers](/categories/utility)

stil/xpath-selector
===================

A library which makes it easy to web scrape HTML or XML pages. Uses XPath queries.

2.0(11y ago)2922.3k↓16.7%1[1 issues](https://github.com/stil/xpath-selector/issues)2HTML

Since Nov 14Pushed 10y ago2 watchersCompare

[ Source](https://github.com/stil/xpath-selector)[ Packagist](https://packagist.org/packages/stil/xpath-selector)[ RSS](/packages/stil-xpath-selector/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (7)Used By (2)

\#XPathSelector ##Description XPathSelector is a libary created for HTML webscraping. It was inspired by Python's Scrapy. It uses PHP DOM extension, so make sure you have it installed. PHP 5.4 is minimum.

\##Installation Recommended way to install XPathSelector is through [Composer](http://getcomposer.org/). Run following command:

```
composer require stil/xpath-selector
```

\###Introduction The starting point of all searches is `XPathSelector\Selector` class. It allows you to load HTML or XML, so you can process it then. There are several methods to do it:

```
use XPathSelector\Selector;
$xs = Selector::load($pathToXml);
$xs = Selector::loadHTMLFile($pathToHtml);
$xs = Selector::loadXML($xmlString);
$xs = Selector::loadHTML($htmlString);
```

Next thing you want to do, is to decide whether you're searching for single DOM element or multiple elements. For single search use `find($query)` method.

```
use XPathSelector\Exception\NodeNotFoundException;

try {
	$element = $xs->find('//head'); // returns first  element found
	echo $element->innerHTML(); // print innerHTML of  tag
} catch (NodeNotFoundException $e) {
	echo $e->getMessage(); // nothing have been found
}
```

And if you need multiple results, use `findAll($query)` instead. This method returns instance of `XPathSelector\NodeListInterface`. Check it out in the API.

```
use XPathSelector\Selector;

$urls = $xs->findAll('//a/@href');
foreach ($urls as $url) {
	echo $url;
}
```

Do you need to check whether XPath path exists? Use `findOneOrNull($query)` method. It returns `Node` object or null, when no results were found. It works just like `find($query)`, except it returns null instead of throwing exception.

```
use XPathSelector\Selector;

$doesExist = $xs->findOneOrNull('//a/@href') !== null;
```

\###sample.xml

```

		Everyday Italian
		Giada De Laurentiis
		2005
		30.00

		Harry Potter
		J K. Rowling
		2005
		29.99

		XQuery Kick Start
		James McGovern
		Per Bothner
		Kurt Cagle
		James Linn
		Vaidyanathan Nagarajan
		2003
		49.99

		Learning XML
		Erik T. Ray
		2003
		39.95

```

\###Search for single result

```
