【问题标题】:How to use Simple HTML DOM PHP to get span data-reactid value?如何使用 Simple HTML DOM PHP 获取 span data-reactid 值?
【发布时间】:2019-10-22 15:12:25
【问题描述】:

这些都不起作用:

$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 获取值。

【问题讨论】:

  • 有没有办法只获取以下值:data-reactid="11"

标签: php html class dom


【解决方案1】:

这行得通。

$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'};

【讨论】:

  • 嗨,马丁,谢谢...但是我剪切并粘贴了您的代码,但它没有返回任何内容?
  • 相同的代码为我返回了 11。你用的是哪个版本的php?
猜你喜欢
  • 2019-06-13
  • 2014-02-07
  • 1970-01-01
  • 1970-01-01
  • 2018-04-24
  • 1970-01-01
  • 1970-01-01
  • 2014-08-22
  • 1970-01-01
相关资源
最近更新 更多