简体   繁体   English

PHP:简单HTML DOM解析器-查找 <p> 标记 <table> 标签

[英]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. 我有一个带有问题和选项的html文件。

在此处输入图片说明

My Doubt: 我的怀疑:

If some questions includes table tag without <p> tag like 如果某些问题包括不带<p>标记的表格标记,例如

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

So my code returns only <p> </p> tag text only. 因此,我的代码仅返回<p> </p>标签文本。 I need the <p> </p> with table </table> tag content also. 我还需要<p> </p>和表</table>标记内容。

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. 首先,您应该学习在代码中添加带有code标签的代码,以便于阅读和回答。 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. 根据您的代码,您的代码仅检查<p部分,如果该部分不存在,则该部分必须位于行的开头,您的代码将跳至下一行。

To solve this you might have to check the <td and then the <p use a if ... else to differentiate between codes. 为了解决这个问题,您可能必须检查<td ,然后<p使用if ... else来区分代码。

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;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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