【问题标题】:Cannot get just the second list on a page with PHP Simple DOM无法使用 PHP Simple DOM 仅获取页面上的第二个列表
【发布时间】:2020-05-28 23:25:32
【问题描述】:

我有这段代码可以尝试在页面上提取列表:

$websiteURL = "https://waset.org/conferences-in-january-2022-in-tokyo";
$html = file_get_html($websiteURL);

foreach ( $html->find( 'ul') as $ul ) {
     foreach($ul->find('li') as $li) {
        echo "LI: " . $li . "<br>";
    }
}

这符合我的预期(即在页面上为所有&lt;ul&gt; 显示每个&lt;li&gt;。

但是,如果我将第二个 foreach 替换为(因为我只想获得第一个列表):

foreach ( $html->find( 'ul', 1) as $ul ) {

我明白了:

"调用 int 上的成员函数 find()"

...这表明find('ul', 1) 没有返回任何内容,但我不知道为什么?

注意:此页面上有两个以上的列表。

有人知道我做错了什么吗?

【问题讨论】:

  • 不,这并不表明,它表明无论您在那里调用该方法,都只是一个整数值开头。你为什么说你替换了 second foreach,那个遍历ul 元素的是你原始代码中的 first 元素?根据文档,find( 'ul', 1) 使用这种方式应该只返回一个对象,而不是一个数组 - 所以仍然试图循环它可能首先没有多大意义。
  • .. 对不起,第一个 foreach!我的坏。
  • 感谢您的回复,我会看看 - 不胜感激
  • 作为旁注:我建议一个更好的选择,改用DOMDocument
  • 我想我的底线问题是如何从网页上的第二个
      访问所有
    • ?

标签: php html dom web-scraping


【解决方案1】:

回答您的问题“我想我的底线问题是如何访问网页上的第二个 &lt;li&gt; 的所有内容?”使用相对现代、得到良好支持并内置于 PHP 中的 API:

<?php
$url = "https://waset.org/conferences-in-january-2022-in-tokyo";

libxml_use_internal_errors(true);
$dom = new DomDocument();
$dom->loadHtmlFile($url);
$lists = $dom->getElementsByTagName("ul");
$items = $lists[1]->getElementsByTagName("li");
foreach ($items as $item) {
    // clean up extra whitespace
    $text = preg_replace("/\s+/", " ", trim($item->textContent));
    echo "$text\n------\n";
}

输出:

ICA 2022: Aeroponics Conference, Tokyo (Jan 07-08, 2022)
------
ICAA 2022: Agroforestry and Applications Conference, Tokyo (Jan 07-08, 2022)
------
ICAAAA 2022: Applied Aerodynamics, Aeronautics and Astronautics Conference, Tokyo (Jan 07-08, 2022)
------
ICAAAE 2022: Aquatic Animals and Aquaculture Engineering Conference, Tokyo (Jan 07-08, 2022)
------
ICAAC 2022: Advances in Astronomical Computing Conference, Tokyo (Jan 07-08, 2022)
------
...

另外值得注意的是,会议名称在&lt;a&gt; 元素中,地点在其中的&lt;span&gt; 中,日期紧随其后。使用它,您可以相当简单地提取数据:

function getNodeText(\DomNode $node): string
{
    $return = "";
    foreach($node->childNodes as $child) {
        if ($child->nodeName === "#text") {
            $return .= trim($child->nodeValue);
        }
    }
    return $return;
}

foreach ($items as $item) {
    $conference = getNodeText($item->getElementsByTagName("a")[0]);
    $location = getNodeText($item->getElementsByTagName("span")[0]);
    $date = getNodeText($item);
    echo "------\n$conference | $location | $date\n";
}

输出:

------
ICA 2022: Aeroponics Conference, | Tokyo | (Jan 07-08, 2022)
------
ICAA 2022: Agroforestry and Applications Conference, | Tokyo | (Jan 07-08, 2022)
------
ICAAAA 2022: Applied Aerodynamics, Aeronautics and Astronautics Conference, | Tokyo | (Jan 07-08, 2022)
------
ICAAAE 2022: Aquatic Animals and Aquaculture Engineering Conference, | Tokyo | (Jan 07-08, 2022)
------
ICAAC 2022: Advances in Astronomical Computing Conference, | Tokyo | (Jan 07-08, 2022)
...

【讨论】:

    猜你喜欢
    • 2014-10-26
    • 2013-03-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-09
    相关资源
    最近更新 更多