【问题标题】:scraping using PHP Simple HTML DOM Parser使用 PHP Simple HTML DOM Parser 进行抓取
【发布时间】:2017-06-08 11:57:04
【问题描述】:

我想使用 PHP 简单的 HTML DOM 解析器从网站上抓取。 源代码就是这么随机:

      <font face="Arial" color="#ff0000">
      <p>Parameters</p>
      </font><font face="Arial" size="2" color="#ff0000">
      <p>Param1</p>
      </font><font face="Arial" size="2" color="#0000ff">
      <p>Details. (Lob., </font><i><font face="Arial"
      size="2" color="#ff0000">Co v</font><font face="Arial" size="2"
      color="#0000ff">.)</p>

不是直接将“Details. (Lob., Co v.)”放在

中,而是使用 放置。 当我使用此代码时
foreach($html->find('p') as $p) 
{
  echo $p->plaintext.'<br>';
}

我找到“Details. (Lob.”),它在找到 或 时停止。 如何提取整行“详细信息。(Lob., Co v.)”

感谢您的回答

【问题讨论】:

  • 你的意思是 "scrape" 吗?只是确保。
  • 是的对不起,我的意思是刮

标签: php html dom web-scraping html-parsing


【解决方案1】:

您可以使用 strip_tags() 函数来删除不必要的标签。去掉不必要的标签后,就可以使用DOM解析器了。

strip_tags() 函数从 HTML、XML 和 PHP 中删除字符串 标签。

string strip_tags (string $str [, string $allowable_tags])

您可以在php.net 上阅读有关 strip_tags() 函数的更多信息

示例:

$html = '<font face="Arial" color="#ff0000">
    <p>Parameters</p>
    </font><font face="Arial" size="2" color="#ff0000">
    <p>Param1</p>
    </font><font face="Arial" size="2" color="#0000ff">
    <p>Details. (Lob., </font><i><font face="Arial"
    size="2" color="#ff0000">Co v</font><font face="Arial" size="2"
    color="#0000ff">.)</p>';

$html = strip_tags($string, '<p>');
echo $html;

结果:

  <p>Parameters</p>

  <p>Param1</p>

  <p>Details. (Lob., Co v.)</p>

【讨论】:

    猜你喜欢
    • 2011-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-12
    • 2012-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多