如何解析HTML/XML并从中提取信息?
它真的有很棒的功能,比如:
foreach($html->find('img') as $element)
echo $element->src . '<br>';
你可以使用simple_html_dom来实现这个目的。然而,我已经用过simple_html_dom了,特别是用于网络爬虫方面,发现它太容易受到攻击了。它可以完成基本工作,但我不建议使用它。
我从未使用过curl来实现这个目的,但我所学到的是,curl可以更有效地完成工作,并且更加稳定。
请查看此链接:scraping-websites-with-curl
QueryPath很不错,但要小心“跟踪状态”,因为如果你没有意识到它的含义,就可能会浪费大量的调试时间来查找发生了什么以及代码为什么无法工作。
它的意思是每次对结果集的调用都会修改对象中的结果集,而不像jquery那样可链接,其中每个链接都是一个新集合,你只有一个单一的集合,即查询结果,并且每个函数调用都会修改该单一集合。
为了获得类似于jquery的行为,您需要在进行过滤/修改操作之前进行分支,这意味着它将更加密切地反映jquery中发生的情况。
$results = qp("div p");
$forename = $results->find("input[name='forename']");
$results
现在包含了 input[name='forename']
的结果集,而不是原始查询 "div p"
。这让我困惑了很久,我发现QueryPath跟踪过滤器、查找和所有修改结果的操作,并将它们存储在对象中。你需要这样做来代替之前的操作。
$forename = $results->branch()->find("input[name='forname']")
那么$results
将不会被修改,您可以再次重复使用结果集,也许有更多知识的人可以澄清一下这一点,但基本上是我所发现的。
我创建了一个名为PHPPowertools/DOM-Query的库,它允许您像使用jQuery一样爬取HTML5和XML文档。
在底层,它使用symfony/DomCrawler将CSS选择器转换为XPath选择器。即使将一个对象传递给另一个对象,它也始终使用同一DomDocument以确保良好的性能。
namespace PowerTools;
// Get file content
$htmlcode = file_get_contents('https://github.com');
// Define your DOMCrawler based on file string
$H = new DOM_Query($htmlcode);
// Define your DOMCrawler based on an existing DOM_Query instance
$H = new DOM_Query($H->select('body'));
// Passing a string (CSS selector)
$s = $H->select('div.foo');
// Passing an element object (DOM Element)
$s = $H->select($documentBody);
// Passing a DOM Query object
$s = $H->select( $H->select('p + p'));
// Select the body tag
$body = $H->select('body');
// Combine different classes as one selector to get all site blocks
$siteblocks = $body->select('.site-header, .masthead, .site-body, .site-footer');
// Nest your methods just like you would with jQuery
$siteblocks->select('button')->add('span')->addClass('icon icon-printer');
// Use a lambda function to set the text of all site blocks
$siteblocks->text(function( $i, $val) {
return $i . " - " . $val->attr('class');
});
// Append the following HTML to all site blocks
$siteblocks->append('<div class="site-center"></div>');
// Use a descendant selector to select the site's footer
$sitefooter = $body->select('.site-footer > .site-center');
// Set some attributes for the site's footer
$sitefooter->attr(array('id' => 'aweeesome', 'data-val' => 'see'));
// Use a lambda function to set the attributes of all site blocks
$siteblocks->attr('data-val', function( $i, $val) {
return $i . " - " . $val->attr('class') . " - photo by Kelly Clark";
});
// Select the parent of the site's footer
$sitefooterparent = $sitefooter->parent();
// Remove the class of all i-tags within the site's footer's parent
$sitefooterparent->select('i')->removeAttr('class');
// Wrap the site's footer within two nex selectors
$sitefooter->wrap('<section><div class="footer-wrapper"></div></section>');
[...]
该库还包括自己的零配置自动加载程序,用于PSR-0兼容库。附带的示例应该可以立即使用,无需进行任何其他配置。或
我编写了一个通用的XML解析器,可以轻松处理GB格式的文件。它基于XMLReader,并且非常易于使用:
$source = new XmlExtractor("path/to/tag", "/path/to/file.xml");
foreach ($source as $tag) {
echo $tag->field1;
echo $tag->field2->subfield1;
}
这是Github仓库的链接: XmlExtractor