标签:document 指定 array com tco his 通过 fun color
<?php function getTagClassContent($url, $tagName, $className) //要抓取的网页, 要抓取的Tag名, 要抓取的Class名 { $doc = new DOMDocument(); @$doc->loadHTML(file_get_contents($url)); $nodes = $doc->getElementsByTagName($tagName); $res = array(); foreach($nodes as $n) { if($attrItemClass = $n->attributes->getNamedItem(‘class‘)) { if($classes = explode(‘ ‘, $attrItemClass->textContent)) { if(in_array($className, $classes)) { $res[] = $n->textContent; } } } } return $res; } $res = getTagClassContent(‘http://www.dyhjw.com/meiyuanzhishu‘, ‘span‘, ‘nom‘); var_dump($res); /* 输出: array(1) { [0]=> string(7) "92.7375" } */
PHP通过DOMDocument对象来抓取网页中的指定class的内容
标签:document 指定 array com tco his 通过 fun color
原文地址:https://www.cnblogs.com/bird-eat-vegetable/p/13983166.html