dongshi4773 2013-10-04 15:19
浏览 94
已采纳

PHP DomXPath - 按类获取子项

So far, my code is getting all classes 'forumRow' using a xPath query. How would I get the href-attribute of the a-element which exists once in every 'forumRow' class?

I'm kinda stuck at the point where I can run a query starting from the result of the first query.

My current code

            $this -> boards = array();
            $html = @file_get_contents('http://www.roblox.com/Forum/Default.aspx');

            libxml_use_internal_errors(true);
            $page = new DOMDocument();
            $page -> preserveWhiteSpace = false;
            $page -> loadHTML($html);

            $xpath = new DomXPath($page);
            $board_array = $xpath -> query('//*[@class="forumRow"]');

            foreach($board_array as $board)
            {
                $childNodes = $board -> childNodes;
                $boardName = $childNodes -> item(0) -> nodeValue;

                if (strlen($boardName) > 0)
                {

                    $boardDesc = $childNodes -> item(1) -> nodeValue;
                    array_push($this -> boards, array($boardName, $boardDesc));
                }
            }
            $Cache -> saveData(json_encode($this -> boards));
  • 写回答

2条回答 默认 最新

  • duanpo1498 2013-10-04 15:48
    关注

    Sad to say, I could not get your code to work (regarding extract of forumRow <td>'s) - so I made this up instead :

    $html = @file_get_contents('http://www.roblox.com/Forum/Default.aspx');
    libxml_use_internal_errors(true);
    $page = new DOMDocument();
    $page->preserveWhiteSpace = false;
    $page->loadHTML($html);
    $xpath = new DomXPath($page);
    
    foreach($xpath->query('//td[@class="forumRow"]') as $element){
        $links=$element->getElementsByTagName('a');
        foreach($links as $a) {
            echo $a->getAttribute('href').'<br>';
        }
    }
    

    produces

    /Forum/Search/default.aspx
    /Forum/ShowForum.aspx?ForumID=46
    /Forum/ShowForum.aspx?ForumID=14
    /Forum/ShowForum.aspx?ForumID=44
    /Forum/ShowForum.aspx?ForumID=43
    /Forum/ShowForum.aspx?ForumID=45
    /Forum/ShowForum.aspx?ForumID=21
    /Forum/ShowForum.aspx?ForumID=13
    ...
    a very long list

    All the hrefs from <td class="forumRow">..<a href= ... ></a>..</td>

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

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条