2017-05-22 68 views
0

我有一個關於數據提取的問題,我也看到很多關於這個問題的主題,但我無法找到任何符合我的要求的解決方案,所以我請求你請幫助我這個錯誤。使用HTML的數據提取DOM

<?php 
    require('admin/inc/simple_html_dom.php'); 

    $html = file_get_contents("http://health.hamariweb.com/rawalpindi/doctors"); 

    $title = $html->find("div#infinite-grid-images", 0)->innertext; 

    echo $title; 

?> 

我想告訴所有這些醫生到我的網站我剛學數據提取,我已經看到了很多的教程,但還是沒有結果,請人誰可以幫我:(

+0

'file_get_contents'返回一個字符串,而不是一個東西。你可以從http://php.net/manual/en/domdocument.loadhtml.php開始。 – chris85

+0

如果我理解正確。你是否試圖從外部網站提取數據? – threeFatCat

+0

是的,正是我想從外部網站提取數據,我也試過file_get_html以及但沒有結果,你能否請寫一段代碼,以便我可以理解我必須寫。 –

回答

0

試裝通過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); 
?> 

您可以決定如何處理數據。

+0

它不工作,當我運行它顯示: 警告:file_get_contents(http://health.hamariweb.com/rawalpindi/doctors):無法打開流:HTTP請求失敗! HTTP/1.1 500內部服務器錯誤在C:\ xampp \ htdocs \ DExplorer \ admin \ inc \ simple \ html_dom.php在線32 沒有發現 –

+0

由於佩德羅洛比託說你需要提供一個用戶代理 – ignatisD

+0

它適用於我:)但我有一個小問題是否有可能我可以從該網頁提取醫生的數據,根據他的專業化我的意思是有很多醫生在那裏被提及有些是牙醫和一些是神經科醫生。所以我的問題是,是否有可能提取牙醫類別的醫生的數據。 還有一件事,我怎麼能得到醫生的照片。 –

0

你想廢網站,返回http 500 error如果沒有用戶代理是用來,繞過這一點,你可以使用curl,即:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://health.hamariweb.com/rawalpindi/doctors"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0"); 
$html = curl_exec($ch); 
curl_close($ch); 
# your code ... 
+0

你能指導我如何動態地從外部網站提取數據。我的意思是, 如果有人在我的網站上使用搜索欄,並輸入關鍵字「最好的牙醫在拉瓦爾品第」。 如果在我的數據庫中沒有與上述關鍵字匹配的數據,那麼我希望我的網絡在Google上搜索該結果。 我希望我的網站能夠動態地從前3個谷歌搜索結果中提取數據。 是否有可能,如果是的話請引導我如何做到這一點。 –