如何找到带有 simple_html_DOM 的标签

how do I find a tag with simple_html_DOM

我正在尝试使用 simple_html_dom 和 php 来解析带有此标签的网页:

<div class="  row  result" id="p_a8a968e2788dad48" data-jk="a8a968e2788dad48" itemscope itemtype="http://schema.org/JobPosting" data-tn-component="organicJob">

其中 data-tn-component="organicJob" 是我要解析的标识符,我似乎无法以 simple_html_dom 识别的方式指定文本。

我沿着这条线尝试了一些事情:

<?PHP
include 'simple_html_dom.php';
$f="http://www.indeed.com/jobs?q=Electrician&l=maine";
    $html->load_file($f);
        foreach($html->find('div[data-tn-component="organicJob"]') as $div)
              {
                 echo  $div->innertext ;
               }
?>

但是解析器没有找到任何结果,即使我知道它们在那里。可能我没有指定我正确找到的东西。 我正在查看 the API,但我仍然不明白如何格式化查找字符串。 我做错了什么?

您的选择器是正确的,但我发现您的代码中存在其他问题

1) 你的包含 include 'simple_html_dom'; 中缺少 .php 它应该是

include '/absolute_path/simple_html_dom.php';

2) 通过 url 加载内容使用 file_get_html 函数而不是 $html->load_file($f); 这是错误的,因为 php 不知道 $html 是 simple_html_dom 对象

$html = file_get_html('http://www.google.com/');
// then only call 
$html->find( ...

3) 在您提供的 link 中:http://www.indeed.com/jobs?q=Electrician+Helper&l=maine 没有具有 data-tn-component 属性的当前元素

所以最终代码应该是

include '/absolute_path/simple_html_dom.php';
$html = file_get_html('http://www.indeed.com/jobs?q=Electrician&l=maine');

$html->load_file($f);
foreach($html->find('div[data-tn-component="organicJob"]') as $div)
{
    echo  $div->innertext ;
}