首页 > 解决方案 > 如何使用 Simple HTML DOM PHP 获取 span data-reactid 值?

问题描述

这些都不起作用:

$html = file_get_html("https://www.example.com/page/");
print($html->find('[data-reactid=10]', 0)->plaintext);
print($html->find('[data-reactid=11]', 0)->plaintext);

html 看起来像这样:

<div class="stuff" data-reactid="10">
<span data-reactid="11">Value I want</span>
</div>

我究竟做错了什么?

供参考。这确实有效:

print($html->find('[data-reactid=5]', 0)->plaintext);`

在哪里:

<div class"stuff" data-reactid="5">
<!-- react-text: 6 -->
Value I want
<!-- /react-text:  -->
</div>

那么如何通过跨度获得值?我可以通过 div 获取值。

标签: phphtmlclassdom

解决方案


这行得通。

$html_str = '
    <div class="stuff" data-reactid="10">
    <span data-reactid="11">Value I want</span>
    </div>
';

// Create a DOM object
$html = new simple_html_dom();

// Load HTML from a string
$html->load($html_str);

// Get the value
echo $html->find('div[data-reactid=10]', 0)->find('span', 0)->{'data-reactid'};

推荐阅读