doufu8887 2012-07-21 16:03
浏览 243
已采纳

使用domDocument获取src元素

I am using domDocument. I am close but need help for the last little bit

I have this html just a snippet below. There are a number of rows. I am trying to get the href.

so far i am doing the following: I can get the table, tr, and td ok , but not sure what to do from there.

Thanks for any help

foreach ($dom->getElementsByTagName('table') as $tableitem) {
    if ( $tableitem->getAttribute('class') == 'tableStyle02'){
        $rows = $tableitem->getElementsByTagName('tr');
        foreach ($rows as $row){ 
            $cols = $row->getElementsByTagName('td'); 

            $hrefs = $cols->item(0)->getElementsByTagName('a'); 
        }     
    }
}

html snippet:

<table width="100%" border="0" cellspacing="0" cellpadding="2" class="tableStyle02"> 
    <tr> 
        <td><span class="Name"><a href="bin.php?cid=703&size=0">
               <strong>Conference Facility</strong></a></span></td>
        <td align="center" nowrap>0.00</td>
        <td align="center">&nbsp;0&nbsp;</td>
        <td align="center">&nbsp;&nbsp;</td>
        <td align="center">&nbsp;0&nbsp;</td>
        <td align="center">&nbsp;0&nbsp;</td>
        <td align="center">&nbsp;0 - 0 &nbsp;</td>
        <td align="center">&nbsp;Wired Internet,&nbsp;&nbsp;&nbsp;</td>
        <td align="center">&nbsp;&nbsp;</td>
    </tr>
  • 写回答

3条回答 默认 最新

  • doutongxuan1614 2012-07-21 16:12
    关注

    Let me introduce you the concept of xpath, a query language for DomDocuments:

    //table[@class="tableStyle02"]//a/@href
    

    Reads as: Take the table tag with class attribute tableStyle02 and then the href attribute from within the a child tag.

    Or as you had the foreach for tr and td elements as well:

    //table[@class="tableStyle02"]/tr/td/a/@href
    

    Now in that path, the a tag is a direct children of the td tag which is a direct children of the tr tag which is a direct children of the table tag. As you can see, with xpath it is much easier to formulate the path to the element than writing everything in PHP code.

    Apropos PHP code, in PHP this can look like:

    $doc = new DOMDocument();
    $doc->loadHTML($html);
    $xp = new DOMXPath($doc);
    $href = $xp->evaluate('string(//table[@class="tableStyle02"]//a/@href)');
    

    The variable $href then contains the string: bin.php?cid=703&size=0.


    This example is with a string (string(...)), so ->evaluate returns a string, which is created from the first found attribute node. Instead you can return a nodelist as well:

    $hrefs = $xp->query('//table[@class="tableStyle02"]/tr/td/span/a/@href');
    #             ^^^^^                                       ^^^^
    

    Now $hrefs contains the usual DOMNodeList, here it contains all the href attribute nodes:

    echo $hrefs->item(0)->nodeValue; # bin.php?cid=703&size=0
    

    Take care that if you use only one slash / to separate tags, that they need to be direct children. With two slashes // it can be a descendant (child or child of child (of child (of ...))).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法