2017-02-21 6 views
0

私はウェブサイトからコンテンツをスクラップしようとしており、一部のサイトで成功しています。しかし、私のコードはflipkart.comのコンテンツをスクラップできません。私はHTML DOM PARSERを使用しています。これは私のコードです。特定のWebサイトのhtml domパーサを使用してコンテンツをスクラップできない

<?php 
include ('simple_html_dom.php'); 
$scrap_url = 'https://www.flipkart.com/lenovo-f309-2-tb-external-hard-disk-drive/p/itmehwha6zkhkgfw'; 
$html = file_get_html($scrap_url); 
foreach($html->find('h1._3eAQiD') as $title_s) 
echo $title_s->plaintext; 
foreach($html->find('div.hGSR34') as $ratings_s) 
echo $ratings_s->plaintext; 
?> 

このコードは空の結果を示しています。誰かが私に問題の内容を教えてもらえますか?このサイトのコンテンツをスクラップする方法はありますか?

+0

コンテンツが窒息している可能性があります。または、いくつかのjsが読み込まれたコンテンツがそこにあると期待するかもしれません。あなたがそれを狭めることができれば、それは私たちを助けるでしょう。 – pguardiario

+0

私はコンテンツがjs-読み込まれていると思います。 PHPでコンテンツをスクラップする方法はありますか? –

+0

最初に[phantomjs](https://phantomjscloud.com/)を実行することができます。あなたが夢中になりたい場合は、いくつかのPHPセレンライブラリもあります。 – pguardiario

答えて

0

このコードは私にとって役に立ちました。

get_content_by_class(curl('https://www.flipkart.com/lenovo-f309-2-tb-external-hard-disk-drive/p/itmehwha6zkhkgfw'), "hGSR34"); 

function curl($url) { 
    $ch = curl_init(); // Initialising cURL 
    //curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 0); 
    curl_setopt($ch, CURLOPT_URL, $url); // Setting cURL's URL option with the $url variable passed into the function 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Setting cURL's option to return the webpage data 
    $data = curl_exec($ch); // Executing the cURL request and assigning the returned data to the $data variable 
    curl_close($ch); // Closing cURL 
    return $data; // Returning the data from the function 
} 

function get_content_by_class($html, $container_class_name) { 

    //preg_match_all('/<div class="' . $container_class_name .'">(.*?)<\/div>/s', $html, $matches); 
    preg_match_all('#<\s*?div class="'. $container_class_name . '\b[^>]*>(.*?)</div\b[^>]*>#s', $html, $matches); 

    // 

    foreach($matches as $match){ 
     $match1 = str_replace('<','&lt',$match); 
     $match2 = str_replace('>','&gt',$match1); 
     print_r($match2); 
    } 

    if (empty($matches)){ 
     echo 'no matches found'; 
     echo '</br>'; 
    } 
    //return $matches; 

} 
+0

あなたのコードを説明して、オペアンプを助けることができますか? – slfan

+0

関数curlは、ページからhtmlを取得し、それを返します。コンテンツ関数を取得すると、クラスごとにHTMLコンテンツを取得します。 – Francesc

関連する問題