我试图从" data-url"中获取价值。来自这样的标签:
<div class="event event-list " data-id="24692" data-url="https://www.example.com/events/20-01-2017-event-x/">
此代码无效:
$urlVariable = ('https://www.example.com/');
$html = file_get_html($urlVariable);
foreach($html->find('[data-url]') as $detailLinks)
print $detailLinks . '<br>';
任何帮助将不胜感激。
答案 0 :(得分:0)
您也可以通过以下方式使用domxpath实现所需的结果:
<?php
$html = '<div class="event event-list " data-id="24692" data-url="https://www.example.com/events/20-01-2017-event-x/">';
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
print $xpath->query('//div[starts-with(@class,"event")]')->item(0)->getAttribute('data-url');
或
print $xpath->query('//div[contains(@class,"event-list")]')->item(0)->getAttribute('data-url');
答案 1 :(得分:0)
您可以找到带有data-url属性的div
foreach($html->find('div[data-url]') as $detailLinks)
echo $detailLinks->{'data-url'};