2017-02-21 72 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硒库。 – 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,并返回它,获取content函数通过类获取内容的html – Francesc