2017-02-21 2 views
0

Я пытался отказаться от содержимого с веб-сайтов и успешно прошел с некоторыми сайтами. Но мой код не может отказаться от контента с flipkart.com. Я использую HTML DOM PARSER, и это мой код.Невозможно отказаться от содержания с помощью анализатора 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 selenium, если вы хотите сходить с ума. – 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