試裝通過file_get_content()返回的字符串
<?php
require('admin/inc/simple_html_dom.php');
$html = file_get_contents("http://health.hamariweb.com/rawalpindi/doctors");
$dom = new simple_html_dom();
$dom->load($html);
$title = $dom->find("#infinite-grid-images", 0)->innertext;
echo $title;
?>
此外,simple_html_dom.php
文件內發貨是一個調用的函數: file_get_html($url)
你可以這樣做:
<?php
require('admin/inc/simple_html_dom.php');
$html = file_get_html("http://health.hamariweb.com/rawalpindi/doctors");
if($html){
$title = $dom->find("#infinite-grid-images", 0)->innertext;
echo $title;
}else{
echo "Nothing found";
}
?>
祝你好運!
curl是你的朋友。
<?php
require('simple_html_dom.php');
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://health.hamariweb.com/rawalpindi/doctors",
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
));
$file = curl_exec($curl);
$error = curl_error($curl);
curl_close($curl);
$dom = new simple_html_dom();
$dom->load($file);
$doctorDivs = $dom->find("#infinite-grid-images", 0)->children();
$doctors = array();
foreach($doctorDivs as $div){
$doctor = array();
$doctor["image"] = "http://health.hamariweb.com/".$div->find('img', 0)->src;
$details = $div->find('table', 1)->find("tr");
$doctor["name"] = trim($details[0]->plaintext);
$doctor["type"] = trim($details[1]->plaintext);
$doctor["etc"] = trim($details[2]->plaintext);
$doctors[] = $doctor;
}
echo "<pre>";
var_dump($doctors);
?>
您可以決定如何處理數據。
'file_get_contents'返回一個字符串,而不是一個東西。你可以從http://php.net/manual/en/domdocument.loadhtml.php開始。 – chris85
如果我理解正確。你是否試圖從外部網站提取數據? – threeFatCat
是的,正是我想從外部網站提取數據,我也試過file_get_html以及但沒有結果,你能否請寫一段代碼,以便我可以理解我必須寫。 –