【问题标题】:Get specific URL with PHP Simple HTML DOM Parser使用 PHP Simple HTML DOM Parser 获取特定 URL
【发布时间】:2016-04-21 16:36:07
【问题描述】:

所以对于我的代码,我基本上需要从网页获取特定的 URL,我使用 Simple HTML DOM Parser,到目前为止,我设法从网页中获取所有链接,类为 .linkify,结果有 18 个不同的链接,我只需要在变量中包含第二个链接。

这是我的代码:

$html = file_get_html("http://saucenao.com/search.php?db=999&url=http://simg4.gelbooru.com//images/4f/3d/$file");
foreach($html->find('a.linkify') as $element)

echo $element->href . '<br>';

我如何使它只生成第二个链接?

谢谢!

【问题讨论】:

    标签: php html parsing dom


    【解决方案1】:

    你可以通过修改来选择你想要的

    $html-&gt;find('a.linkify')

    $html-&gt;find('a.linkify', 1)

    要获取第二个,我们使用,1。它是基于索引的,所以我们从 0 开始(0 = 第一个,1 = 第二个)

    要在一行中获取href,请执行以下操作:

    $html-&gt;find('a.linkify', 1)-&gt;href

    【讨论】:

    • 所以我只是按照你说的做了,页面上似乎什么都没有出现,没有错误代码,只是一个空白页面。这是我的代码:$html = file_get_html("http://saucenao.com/search.php?db=999&amp;url=http://simg4.gelbooru.com//images/4f/3d/$file"); foreach($html-&gt;find('a.linkify', 1) as $element) echo $element-&gt;href . '&lt;br&gt;';
    猜你喜欢
    • 2012-08-12
    • 2012-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-08
    相关资源
    最近更新 更多