duanliang1019 2015-02-04 22:07
浏览 103
已采纳

如果<table>包含<a>,则首先获取<tr>&<a>

I'm stuck on a Simple HTML Dom question...

For each <table> on my webpage I'd like to scan the <table> and check if there is an <a> inside of it. If there is, then echo the first <tr> and <a> else skip and go on to the end <table>.

I started working on it but I'm unsure how to progress or check if there is a <a> in the table...

foreach ($html->find('table') as $name) {
    foreach ($name->find('a') as $elem) {
        echo $elem->plaintext.'<br>';
    }
}

Any ideas?

  • 写回答

1条回答 默认 最新

  • doumu5662 2015-02-04 22:19
    关注

    The code you've shared seems very close. See below to output the first child <tr> of the table, if an <a> element is found, then stop looping through additional <a> elements. What specific problems are you having with your code?

    foreach ($html->find('table') as $name) {
        foreach ($name->find('a') as $elem) {
            echo $name->find('tr', 0)->plaintext; // echo first TR element
            echo $elem->plaintext.'<br>'; // echo A element
            break 1; // skip to the next table
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?