简单 HTML DOM 解析器 - 使用随机数查找 class
Simple HTML DOM Parser - find class with random number
我正在尝试从一个网站抓取数据。我坚持收视率。
他们有这样的东西:
<div class="rating-static rating-10 margin-top-none margin-bottom-sm"></div>
<div class="rating-static rating-13 margin-top-none margin-bottom-sm"></div>
<div class="rating-static rating-46 margin-top-none margin-bottom-sm"></div>
其中 rating-10
实际上是一颗星,在我的情况下 rating-13
两颗星,rating-46
在我的脚本中将是五颗星。
评分范围可以是 0-50。
我的计划是创建 switch
,如果我得到 class 范围从 1-10 我会知道这是一颗星,从 11-20 两颗星等等。
任何想法,任何帮助将不胜感激。
试试这个
<?php
$data = '<div class="rating-static rating-10 margin-top-none margin-bottom-sm"></div>';
$dom = new DOMDocument;
$dom->loadHTML($data);
$xpath = new DomXpath($dom);
$div = $dom->getElementsByTagName('div')[0];
$div_style = $div->getAttribute('class');
$final_data = explode(" ",$div_style);
echo $final_data[1];
?>
这将为您提供预期的输出。
我有一个类似的项目,如果你想解析整个 HTML 站点,这应该是这样做的方法
$dom = new DOMDocument();
$dom->loadHTML($html); // The HTML Source of the website
foreach ($dom->getElementsByTagName('div') as $node){
if($node->getAttribute("class") == "rating-static"){
$array = explode(" ", $node->getAttribute("class"));
$ratingArray = explode("-", $array[1]); // $array[1] is rating-10
//$ratingArray[1] would be 10
// do whatever you like with the information
}
}
可能是您必须将 if
部分更改为 strpos
检查,我还没有测试过这个脚本,但我认为 getAttribute("class")
returns全部 类。这将是 if 语句 then
if(strpos($node->getAttribute("class"), "rating-static") !== false)
仅供参考,我尝试使用 Querypath 来满足未来的解析需求。它只是 PHP DOM 解析器的包装器,并且工作得非常好。
我正在尝试从一个网站抓取数据。我坚持收视率。 他们有这样的东西:
<div class="rating-static rating-10 margin-top-none margin-bottom-sm"></div>
<div class="rating-static rating-13 margin-top-none margin-bottom-sm"></div>
<div class="rating-static rating-46 margin-top-none margin-bottom-sm"></div>
其中 rating-10
实际上是一颗星,在我的情况下 rating-13
两颗星,rating-46
在我的脚本中将是五颗星。
评分范围可以是 0-50。
我的计划是创建 switch
,如果我得到 class 范围从 1-10 我会知道这是一颗星,从 11-20 两颗星等等。
任何想法,任何帮助将不胜感激。
试试这个
<?php
$data = '<div class="rating-static rating-10 margin-top-none margin-bottom-sm"></div>';
$dom = new DOMDocument;
$dom->loadHTML($data);
$xpath = new DomXpath($dom);
$div = $dom->getElementsByTagName('div')[0];
$div_style = $div->getAttribute('class');
$final_data = explode(" ",$div_style);
echo $final_data[1];
?>
这将为您提供预期的输出。
我有一个类似的项目,如果你想解析整个 HTML 站点,这应该是这样做的方法
$dom = new DOMDocument();
$dom->loadHTML($html); // The HTML Source of the website
foreach ($dom->getElementsByTagName('div') as $node){
if($node->getAttribute("class") == "rating-static"){
$array = explode(" ", $node->getAttribute("class"));
$ratingArray = explode("-", $array[1]); // $array[1] is rating-10
//$ratingArray[1] would be 10
// do whatever you like with the information
}
}
可能是您必须将 if
部分更改为 strpos
检查,我还没有测试过这个脚本,但我认为 getAttribute("class")
returns全部 类。这将是 if 语句 then
if(strpos($node->getAttribute("class"), "rating-static") !== false)
仅供参考,我尝试使用 Querypath 来满足未来的解析需求。它只是 PHP DOM 解析器的包装器,并且工作得非常好。