【问题标题】:How to get <a href= value inside div with class name only?如何仅使用类名在 div 中获取 <a href= 值?
【发布时间】:2016-12-16 01:38:33
【问题描述】:

我试图在类名 (class="visible-xs") 的 div 中获取 href 的值。我尝试了这段代码,它也得到了 div 之外的所有 href,这是我不想要的。

$dom = new DOMDocument;
$dom->loadHTML($code2);
foreach ($dom->getElementsByTagName('a') as $node)
{

 echo $node->getAttribute("href")."\n";
}

然后我尝试了以下操作,但它给了我错误(致命错误:调用未定义的方法 DOMDocument::getElementsByClassName() in..):

$dom = new DOMDocument;
$dom->loadHTML($code2);
foreach ($dom->getElementsByClassName('visible-xs') as $bigDiv) {

   echo $bigDiv->getAttribute("href")."\n";
}

任何人都可以帮我解决上述错误,并且只获取类名称为 visible-xs 的 div 内的 href 值吗?提前致谢。

样本数据:

<tr class="ng-scope" ng-repeat="item in itemContent">
<td class="ng-binding" style="word-wrap: break-word;">test/folder/1.mp4
<div class="visible-xs" style="padding-top: 10px;">
<!-- ngIf: item.isViewable --> class="btn btn-default ng-scope" ng-click="$root.openView(item);">View</a><!-- end ngIf: item.isViewable -->
<a href="https://somesite.com/test/1.mp4" class="btn btn-default" ng-href="https://somesite.com/test/1.mp4" target="_blank">Download</a>
<a class="btn btn-default" href="javascript:void(0);" ng-click="item.upload()" target="_blank">Upload</a>
</div>
</td>
<!-- ngIf: hasViewables --><td class="text-right hidden-xs ng-scope" style="white-space: nowrap; width: 60px;" ng-if="hasViewables">
<!-- ngIf: item.isViewable -->class="btn btn-default ng-scope" ng-click="$root.openView(item);">View</a><!-- end ngIf: item.isViewable -->
</td><!-- end ngIf: hasViewables -->
<td class="text-right hidden-xs" style="white-space: nowrap; width: 250px;">
<a href="https://somesite.com/test/1.mp4" class="btn btn-default" ng-href="https://somesite.com/test/1.mp4" target="_blank">Download</a>
javascript:void(0);" ng-click="item.upload()" target="_blank">Upload</a>
</td>
</tr>

【问题讨论】:

  • 感谢您的回复。您能给我展示一个如何使用 xpath 来实现此目的的示例吗?
  • 我尝试了以下但没有数据:$doc = new DOMDocument(); @$doc->loadHTML($code2); $xpath = new DomXPath($doc); foreach ($xpath->query('//div[class="visible-xs"]//a[@href]') as $link) { echo $link->getAttribute('href'); }
  • div类名是visible-xs,div里面的somesite.com/test/1.mp4 一次而不是多次
  • 我在第一篇文章中更新了示例数据。不幸的是,数据是这样呈现的。数据来自远程网站,我无法控制它如何呈现数据!我只想获取此行的 href 值 somesite.com/test/1.mp4" class="btn btn-default" ng-href="somesite.com/test/1.mp4" target="_blank">下载 javascript:void (0);" ng-click="item.upload()" target="_blank">上传

标签: php parsing dom href


【解决方案1】:

没有getElementsByClassName 函数。遍历您的 div,检查类,如果匹配,则将链接拉入内部并输出您想要的 hrefs(如果您想在第一次匹配后停止,则中断)。

$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($html);
libxml_clear_errors();
foreach ($dom->getElementsByTagName('div') as $div) {
     if($div->getattribute('class') == 'visible-xs') {
          foreach($div->getElementsByTagName('a') as $link) {
               echo $link->getattribute('href');
          }
     }
}

演示:https://eval.in/698484

breakhttps://eval.in/698488 为例。

【讨论】:

  • 非常感谢。那个演示帮助了我很多,但我怎么能忽略 上传 ? (我的错误我忘记更正示例数据)
  • 当然你可以使用strpospreg_match 来阻止hrefs 是javascript。按照我检查课程的方式进行。
猜你喜欢
  • 2020-02-27
  • 2021-01-10
  • 2015-12-04
  • 1970-01-01
  • 1970-01-01
  • 2023-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多