尝试使用简单的 html dom 抓取 kickasstorrents

Trying to scrape kickasstorrents with simple html dom

我正在尝试使用简单的 html dom 抓取 kickasstorrents,但出现错误,我什至还没有开始。我遵循了一些简单的 html 教程并且设置了我的 url 并使用 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;
?>

我收到错误

Fatal error: Call to a member function clear() on resource in C:\wamp64\www\index.php on line 36

我做错了什么? 谢谢

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

替换

$kat = file_get_contents($html);

$kat = file_get_html($html);

为什么你 returning $ret; 作为你问题中的代码。你的函数 scrap_kat()

中没有变量 $ret

你可以 return $kat 而不是 $ret 而不要 unset($kat);

Simple_html_dom 是一个 class。在那个 class 中可能有一个函数调用,clear 或者它在 Simple_html_dom_node class 中。但是在简单的htmldom中,你需要使用simple_html_domclass。

@Hassaan,是正确的。 file_get_contents是一个原生的php函数,你必须创建一个simple_html_domclass的对象。喜欢,

$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();

好吧,你的代码中有很多错误,所以我只是告诉你如何做到这一点。如果需要解释,请在此答案下方发表评论。我会的。