webignition / html-document-link-finder
在给定的HTML文档中查找锚点URL
5.0
2019-04-03 10:47 UTC
Requires
- php: >=7.2.0
- webignition/absolute-url-deriver: >=3,<4
- webignition/uri: >=0.1,<1
- webignition/web-page-model: >=5.7,<6
Requires (Dev)
- mockery/mockery: ^1
- phpstan/phpstan: ^0.11.5
- phpstan/phpstan-mockery: ^0.11.0
- phpunit/phpunit: ^7
- squizlabs/php_codesniffer: ^3
README
获取HTML文档中链接的绝对URL集合及其关联元素。
用法
从Web页面获取LinkCollection
use webignition\HtmlDocumentLinkUrlFinder\HtmlDocumentLinkUrlFinder; use webignition\WebResource\WebPage\WebPage; $webPageUrl = 'http://www.google.co.uk/search?q=Hello+World'; $webPage = WebPage::createFromContent((string) file_get_contents($sourceUrl)); $finder = new HtmlDocumentLinkUrlFinder(); $linkCollection = $finder->getLinkCollection($webPage, $webPageUrl);
访问LinkCollection
use Psr\Http\Message\UriInterface; // Assuming $linkCollection from previous example // Iterating foreach ($linkCollection as $link) { $link->getUri(); // UriInterface instance $link->getElement(); // \DOMElement instance } // Counting count($linkCollection); // Get URIs only $linkCollection->getUris(); // array of UriInterface // Get unique URIs only $linkCollection->getUniqueUris(); // array of UriInterface
过滤LinkCollection
所有LinkCollection::filterBy*()
方法都返回一个新的LinkCollection
实例。
use webignition\Uri\ScopeComparer; // Filtering $anchorLinks = $linkCollection->filterByElementName('a'); $elementsWithRelStylesheetAttribute = $linkCollection->filterByAttribute('rel', 'stylesheet'); $linksWithinUrlScope = $linkCollection->filterByUrlScope( new ScopeComparer(), ['http://example.com/'] ); $linkElementsWithRelStylesheetAttribute = $linkCollection ->filterByElementName('link') ->filterByAttribute('rel', 'stylesheet');