简体   繁体   中英

PHP: Simple HTML DOM Parser - find <p> tag with <table> tag

I am facing web scraping functionality issue.

I have a html file with questions and options.

在此处输入图片说明

My Doubt:

If some questions includes table tag without <p> tag like

检查我的问题html文件file.html

So my code returns only <p> </p> tag text only. I need the <p> </p> with table </table> tag content also.

Please clarify me, how to solve this issue?

Thank you

First you should learn to add your code in the post with in code tags which will be easier for use to read and answer. According to your code your code only check for <p part and that part has to be at the start of the line if it's not there your code will skip to the next.

To solve this you might have to check the <td and then the <p use a if ... else to differentiate between codes.

With simple you would have to do something like this:

foreach($html->find('p') as $p){
  if($p->nextSibling() && $p->nextSibling()->tag == 'table'){
    $table = $p->nextSibling();
    echo $p;
    echo $table;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM