【问题标题】:Parsing only text content from url仅解析来自 url 的文本内容
【发布时间】:2013-09-27 09:35:34
【问题描述】:

我正在尝试从给定的 url 解析文本内容。代码如下:

<?php
$url = 'http://stackoverflow.com/questions/12097352/how-can-i-parse-dynamic-content-from-a-web-page';
$content = file_get_contents($url);
echo $content;                          // This parse everything on the page, including image + everything

$text=escapeshellarg(strip_tags($content));
echo "</br>";
echo $text;   // This gives source code also, not only the text content over page
?>

我只想获取写在页面上的文本。没有页面源代码。有什么想法吗?我已经用谷歌搜索过了,但上面的方法只存在于任何地方。

【问题讨论】:

  • 您使用escapeshellarg的原因是什么?
  • @CORRUPT:我已经用过了。它还提供源代码。不仅是写在页面上的文字。
  • 我将整个内容作为参数传递给 c++ 程序。 escapeshellarg 引用 '' 的结果,以便它可以作为单个参数遇到

标签: php


【解决方案1】:

您可以使用DOMDocumentDOMNode

$doc = new DOMDocument();
$doc->loadHTMLFile($url);
$xpath = new DOMXPath($doc);
foreach($xpath->query("//script") as $script) {
    $script->parentNode->removeChild($script);
}
$textContent = $doc->textContent; //inherited from DOMNode

除了使用 xpath,您还可以这样做:

$doc = new DOMDocument();
$doc->loadHTMLFile($url); // Load the HTML
foreach($doc->getElementsByTagName('script') as $script) { // for all scripts
    $script->parentNode->removeChild($script); // remove script and content 
                                               // so it will not appear in text
}
$textContent = $doc->textContent; //inherited from DOMNode, get the text.

【讨论】:

  • 谢谢,但我们仍然得到 ` "https:") { p += "s"; d = "引擎"; } var z = document.createElement("script"); z.type = "文本/javascript"; z.async = 真; z.src = p + "://" + d + ".adzerk.net/ados.js"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(z, s);变种阿多斯=阿多斯|| {}; ados.run = ados.run || []; ados.run.push(function () {ados_setKeywords('java,javascript,html-parsing,jsoup,dynamic-data');;ados_load();}); var _gaq=_gaq||` 等等
  • @Karimkhan 是&lt;script&gt;标签的文本内容。如果您不想要它,则必须手动删除它,例如 XPath。请参阅编辑后的答案。根据网站的不同,您可能还需要删除其他不需要的标签。
  • 我必须保持概括,因为用户可以输入任何网址!
  • @jaudette:为什么它会发出一堆警告? `arning: DOMDocument::loadHTMLFile(): htmlParseEntityRef: 期待 ';'在stackoverflow.com/questions/12097352/… 中,第 13 行 /opt/lampp/htdocs/FB/ec2/test.php 中的第 255 行警告:DOMDocument::loadHTMLFile(): htmlParseEntityRef: 期待 ';'在stackoverflow.com/questions/12097352/… 中,第 13 行 /opt/lampp/htdocs/FB/ec2/test.php 中的第 255 行警告:DOMDocument::loadHTMLFile(): htmlParseEntityRef: 期待 ';'在 htt `等等等等
  • @Karimkhan 每个网址的警告是否相同,还是特定于您的页面?第 13 行是什么?根据文档:While malformed HTML should load successfully, this function may generate E_WARNING errors when it encounters bad markup. libxml's error handling functions may be used to handle these errors.
【解决方案2】:
$content = file_get_contents(strip_tags($url));

这将删除来自页面的 HTML 标记

【讨论】:

    【解决方案3】:

    要删除 html 标记,请使用:

    $text = strip_tags($text);
    

    【讨论】:

      【解决方案4】:

      一个简单的 cURL 就可以解决这个问题。 [测试]

      <?php
      $ch = curl_init("http://stackoverflow.com/questions/12097352/how-can-i-parse-dynamic-content-from-a-web-page");
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //Sorry forgot to add this
      echo strip_tags(curl_exec($ch));
      curl_close($ch);
      ?>
      

      【讨论】:

      • 谢谢,但它仍然提供类似 ` $('#tell-me-more').click(function () { var clickSource = $("body").attr("class") 的代码+ '-mini'; if ($("body").hasClass("questions-page")) { clickSource = 'questionpagemini'; } if ($("body").hasClass("home-page")) { clickSource = 'homepagemini'; } StackExchange.using("gps", function () { StackExchange.gps.track("aboutpage.click", { aboutclick_location: clickSource } , ` 等等。不应该出现
      • 你需要做很多解析才能得到数据。在此之前,您需要获得 SO 的许可,因为您在不使用任何 API 的情况下使用他们的资源,因为这是“Leeching”的同义词
      • 它不仅如此,任何网址都可以在那里!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-15
      • 1970-01-01
      • 2013-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多