如何在PHP中回显一个被删除的div?

时间:2013-04-18 00:46:08

标签: php curl foreach web-scraping screen-scraping

如何回应和刮擦div类?我试过这个,但它不起作用。我正在使用cURL来建立连接。我怎么回应呢?我想要它在实际页面上的样子。     $ document = new DOMDocument();     $文档 - > loadHTML($ HTML);     $ selector = new DOMXPath($ document);     $ anchors = $ selector-> query(“/ html / body // div [@ class ='resultitem']”);     //您要检索的网址

foreach($anchors as $a) { 
    echo $a;
}

1 个答案:

答案 0 :(得分:3)

邻居, 我刚刚在下面创建了这个片段,它使用了你的逻辑,并进行了一些调整,以便在get_contents函数的网页中显示指定的类。 也许你可以插入你的价值并尝试一下?

(注意:我将错误检查放在那里以查看一些错误。在调整时使用它会很有帮助。)

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

$url = "http://www.tizag.com/cssT/cssid.php";
$class_to_scrape="display";

$html = file_get_contents($url);
$document = new DOMDocument(); 
$document->loadHTML($html); 
$selector = new DOMXPath($document); 

$anchors = $selector->query("/html/body//div[@class='". $class_to_scrape ."']");

echo "ok, no php syntax errors. <br>Lets see what we scraped.<br>";

foreach ($anchors as $node) {
    $full_content = innerHTML($node);
   echo "<br>".$full_content."<br>" ;
}

/* this function preserves the inner content of the scraped element. 
** http://stackoverflow.com/questions/5349310/how-to-scrape-web-page-data-without-losing-tags
** So be sure to go and give that post an uptick too:)
**/
function innerHTML(DOMNode $node)
{
  $doc = new DOMDocument();
  foreach ($node->childNodes as $child) {
    $doc->appendChild($doc->importNode($child, true));
  }
  return $doc->saveHTML();
}


?>