大家似乎都在这里碰壁,我试着创建一个简单的脚本,通过抓取页面计算当前运行此地图的服务器数量,使用类“.row ark_srv1”计算div,然后回显那个数字。
问题:脚本返回0
到目前为止,我已经成功打了一遍:
<?php
$html_string = file_get_contents('toparkservers.com/1/search/?term=Umassoura');
function getElementsByClassName($elements, $className) {
$matches = array();
foreach($elements as $element) {
if (!$element->hasAttribute('class')) {
continue;
}
$classes = preg_split('/\s+/', $element->getAttribute('class'));
if ( ! in_array($className, $classes)) {
continue;
}
$matches[] = $element;
}
return $matches;
}
$dom = new DOMDocument;
$dom->loadHTML($html_string);
$divs = getElementsByClassName($dom->getElementsByTagName('.row ark_srv1'), '.row ark_srv1');
$length = $divs->length;
echo count($divs);
?>
答案 0 :(得分:4)
作为替代方案,为什么不使用xpath
按类名获取元素:
$html_string = file_get_contents('http://toparkservers.com/1/search/?term=Umassoura');
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($html_string);
libxml_clear_errors();
$xpath = new DOMXpath($dom);
$class = 'row ark_srv1';
$elements = $xpath->query("//*[contains(@class, '{$class}')]");
echo 'elements found: ', $elements->length;
答案 1 :(得分:0)
当我解析HTML时,我喜欢使用 XPath
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$divs= $xpath->query("//div[@class='className']");
echo count($divs);
答案 2 :(得分:0)
您可能希望使用QueryPath:
<?php
print html5qp('http://toparkservers.com/1/search/?term=Umassoura', '.row.ark_srv1')->length;
// 9