PHP通过DOMDocument对象来抓取网页中的指定class的内容
2020-12-22 15:26
标签:document 指定 array com tco his 通过 fun color PHP通过DOMDocument对象来抓取网页中的指定class的内容 标签:document 指定 array com tco his 通过 fun color 原文地址:https://www.cnblogs.com/bird-eat-vegetable/p/13983166.htmlphp
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"
}
*/
上一篇:java单体锁分类
下一篇:jQuery学习01
文章标题:PHP通过DOMDocument对象来抓取网页中的指定class的内容
文章链接:http://soscw.com/index.php/essay/37595.html