您可以使用
PHP Simple DOM Parser抓取页面并轻松选择部分页面.
一样容易:
$html = file_get_html('http://www.google.com/');
$ret = $html->find('div[id=foo]');
如果您要执行的操作是获取http://www.freeoh.net/的标题,则以下代码将起作用.您需要在与以下脚本相同的文件夹中放置simple_html_dom.php和一个名为page.txt的文件(确保该脚本具有读取和写入的权限). (我假设您已经启用了cURL,正如您在问题中提到的那样.)
include 'simple_html_dom.php';
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, "http://www.freeoh.net/");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_REFERER, "http://www.freeoh.net/");
$result = curl_exec ($curl);
curl_close ($curl);
//write contents of $result to file
$File = "page.txt";
$fh = fopen($File, 'w') or die("can't open file");
fwrite($fh, $result);
fclose($fh);
//turn file into dom object
$page = file_get_html("page.txt");
$header = $page->find("div", 1);
echo $header;
?>
这有点hacky因为我使用cURL来抓取页面,然后需要将它存储在某处,以便PHP Simple HTML Dom解析器能够正确解析它,但它可以工作.