我必须从网站的div中恢复一些文本。 div的结构如下:
HTML标记:
<div class="content" id="content">
Loading.....
</div>
DIV的内容通过AJAX函数改变,这是我猜的页面上载。并且DIV的内容在1或2秒后得到改变.HTML结构变为:
<div class="content" id="content">
<span class"parent">
<span class="child">
<span class="sometext">HERE IS SOME TEXT</span>
</span>
</span>
</div>
当我使用以下PHP函数(crawl_page)来获取带有ID内容的div的HTML时,它总是返回(正在加载..)它应该是。
我需要的是更新后的html代码,无论如何都要实现这个目标吗?
function crawl_page($url)
{
$doc = new DOMDocument();
$doc->loadHTMLFile($url);
$xpath = new DOMXPath($doc);
$element = $xpath->query("//*[@id='content']")->item(0);
echo $element->nodeValue;
}
crawl_page("http://example.com/#1:7");
答案 0 :(得分:3)
我希望它的工作。并从以下网址下载包含文件
http://sourceforge.net/projects/simplehtmldom/files/
<?php
// example of how to use basic selector to retrieve HTML contents
include('../simple_html_dom.php');
// get DOM from URL or file
$html = file_get_html('http://example.com/#1:7');
// find all link
foreach($html->find('a') as $e)
echo $e->href . '<br>';
// find all image
foreach($html->find('img') as $e)
echo $e->src . '<br>';
// find all image with full tag
foreach($html->find('img') as $e)
echo $e->outertext . '<br>';
// find all div tags with id=gbar
foreach($html->find('div#content') as $e)
echo $e->innertext . '<br>';
// find all span tags with class=gb1
foreach($html->find('span.gb1') as $e)
echo $e->outertext . '<br>';
// find all td tags with attribite align=center
foreach($html->find('td[align=center]') as $e)
echo $e->innertext . '<br>';
// extract text from table
echo $html->find('td[align="center"]', 1)->plaintext.'<br><hr>';
// extract text from HTML
echo $html->plaintext;
?>