我如何使用此 HTML 代码解析我想要的内容?

How can i parse what i want with this HTML code?

我想从此 HTML 代码中提取属性“href”和“title”:

<span class='ipsType_break ipsContained'>
  <a 
    href='https://www.xxxx/topic/11604/' 
    class='' title='[2002] Le Boulet [xxxx] '
    data-ipsHover data-ipsHover-target='https://www.xxxxx/topic/1160'
    data-ipsHover-timeout='1.5'>
<span>[2002] Le Boulet [xxxxx]</span>
  </a>
</span>

我在 PHP 中尝试了一些代码,但它不起作用 :(

举例

$e = $html->find('span[class=ipsType_break ipsContained]');
$value = $e->title;
print_r($value);

如果您想使用 类 查找 HTML 元素,您可以像 CSS 中那样使用 dot notation:

$e = $html->find('span.ipsType_break.ipsContained');

您实际上希望这些属性形成 span 内的 a 元素。您正确地找到了 span 标签,但是您使用 returns 的语句是您只需要第一个元素的数组。然后你应该搜索这个 span 标签的 children 来提取它的第一个 a child (同样,因为 find returns 一个元素数组如果只有一个元素与您的选择器匹配则事件):

$a = $html->find('span[class=ipsType_break ipsContained]', 0)->find('a', 0);
print_r (['title' => $a->title, 'href' => $a->href]);

输出是:

Array
(
    [title] => [2002] Le Boulet [xxxx] 
    [href] => https://www.xxxx/topic/11604/
)

你可以使用find单次调用,但是你必须指明你要找的是anchora,因为这里的span没有title属性

作为 find returns 数组,您必须通过将 0 指定为第二个参数来表明您想要第一个元素。

$e = $html->find('span[class=ipsType_break ipsContained] a', 0);
echo $e->href . PHP_EOL;
echo $e->title;

输出

https://www.xxxx/topic/11604/
[2002] Le Boulet [xxxx]