试图用简单的html dom刮掉kickasstorrents

时间:2016-12-18 11:28:49

标签: php web-scraping simple-html-dom

我试图用简单的html dom刮掉kickasstorrents,但我收到一个错误,我还没有开始。我按照一些简单的html教程,设置了我的网址并使用了curl。

代码如下:

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

function scrap_kat() {

// initialize curl
$html = 'http://katcr.to/new/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $html);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$ip=rand(0,255).'.'.rand(0,255).'.'.rand(0,255).'.'.rand(0,255);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("REMOTE_ADDR: $ip", "HTTP_X_FORWARDED_FOR: $ip"));
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/".rand(3,5).".".rand(0,3)." (Windows NT ".rand(3,5).".".rand(0,2)."; rv:2.0.1) Gecko/20100101 Firefox/".rand(3,5).".0.1");
$html2 = curl_exec($ch);
if($html2 === false)
{
    echo 'Curl error: ' . curl_error($ch);
}
else
{
    // create HTML DOM
    $kat = file_get_contents($html);
}
curl_close($ch);

// scripting starts




// clean up memory
$kat->clear();
unset($kat);
// return information
return $ret;

}
$ret = scrap_kat();
echo $ret;
?>

我收到错误

  

致命错误:在第36行的C:\ wamp64 \ www \ index.php中调用资源上的成员函数clear()

我做错了什么? 感谢。

2 个答案:

答案 0 :(得分:0)

file_get_contents是PHP的内置函数。对于简单的html dom,您可以使用file_get_html

替换

$kat = file_get_contents($html);

$kat = file_get_html($html);

为什么要在问题中返回$ret;作为您的代码。函数$ret

中没有变量scrap_kat()

您可以返回$kat而不是$ret而不是unset($kat);

答案 1 :(得分:0)

Simple_html_dom是一个类。在该类中,可能有一个函数调用,clear或它在Simple_html_dom_node类中。但是在简单的html dom中,你需要使用simple_html_dom类。

@Hassaan,是对的。 file_get_contents是一个原生的php函数,你必须创建一个simple_html_dom类的对象。像,

$html = new simple_html_dom();

使用以下代码。

function scrap_kat() {
$url = 'http://katcr.to/new/';
// $timeout= 120;
# create object
$html = new simple_html_dom();
#### CURL BLOCK ####
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/".rand(3,5).".".rand(0,3)." (Windows NT ".rand(3,5).".".rand(0,2)."; rv:2.0.1) Gecko/20100101 Firefox/".rand(3,5).".0.1");
//curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
$ip=rand(0,255).'.'.rand(0,255).'.'.rand(0,255).'.'.rand(0,255);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("REMOTE_ADDR: $ip", "HTTP_X_FORWARDED_FOR: $ip"));
$content = curl_exec($curl);
curl_close($curl);
# note the variable change.
# load the curl string into the object.
$html->load($content);
//echo $ip;
#### END CURL BLOCK ####
print_r($html->find('a'));
// clean up memory
$html->clear();
unset($html);
}
scrap_kat();

嗯,他们的代码中有很多错误,所以我只是告诉你如何做到这一点。如果需要说明,请在下面的答案中评论。我会。