【问题标题】:How to get all the link present in the div tag using Goutte如何使用 Goutte 获取 div 标签中存在的所有链接
【发布时间】:2016-10-23 02:40:00
【问题描述】:

我正在尝试获取 div 标签内的所有链接。 我能够得到所有的名字,但也想得到链接。 示例 HTML:

<li id="u_1y_y" class="friendBrowserListUnit">
<div class="clearfix">
<a class="_8o _8t lfloat _ohe" aria-hidden="true" tabindex="-1" href="/profile.php?id=100004732455685&fref=pymk" role="presentation">
<div class="clearfix _42ef">
<div class="rfloat _ohf">
<div class="friendBrowserContentAlignMiddle">
<div class="friendBrowserNameTitle fsl fwb fcb">
<a href="/profile.php?id=100004732455685&hc_location=friend_browser&fref=pymk">Jeya Kumar</a>
</div>
<div class="friendBrowserMarginTopMini"></div>
<div class="fsm fwn fcg">
</div>
</div>
</div>
</li>
<li id="u_1y_y" class="friendBrowserListUnit">
<div class="clearfix">
<a class="_8o _8t lfloat _ohe" aria-hidden="true" tabindex="-1" href="/profile.php?id=100004732455&fref=pymk" role="presentation">
<div class="clearfix _42ef">
<div class="rfloat _ohf">
<div class="friendBrowserContentAlignMiddle">
<div class="friendBrowserNameTitle fsl fwb fcb">
<a href="/profile.php?id=100004732&hc_location=friend_browser&fref=pymk">Aman Kumar</a>
</div>
<div class="friendBrowserMarginTopMini"></div>
<div class="fsm fwn fcg">
</div>
</div>
</div>
</li>

我尝试了什么:

$message = $crawler->filter('div.friendBrowserNameTitle.fsl.fwb.fcb');
foreach ($message as $key) {
    echo $key->textContent . '<br>';
}

输出:

Jeya Kumar
Aman Kumar

但是如何获取链接:

/profile.php?id=100004732455685&hc_location=friend_browser&fref=pymk
/profile.php?id=100004732&hc_location=friend_browser&fref=pymk

【问题讨论】:

    标签: php laravel-5.2 goutte


    【解决方案1】:

    当用户使用 Goutte 发出请求时,会返回一个 Symfony\Component\DomCrawler 实例。 Crawler 对象提供Crawler::links() 实用方法,提供给定爬虫对象内的所有链接。

    Crawler::links() 方法将返回在其中找到的链接数组。

    $links = $crawler->links();
    foreach ($links as $link) {
        echo $link->getUri();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-22
      • 1970-01-01
      • 2019-11-07
      • 2013-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-27
      相关资源
      最近更新 更多