这是我的PHP代码
<?php
$agents = array(
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1',
'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.9) Gecko/20100508 SeaMonkey/2.0.4',
'Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)',
'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; da-dk) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1');$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: en-us,en;q=0.5";
$header[] = "Pragma: ";//assign to the curl request.
$url = 'http://ball-control.com';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl,CURLOPT_USERAGENT,$agents[array_rand($agents)]); //random agent
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);//set header
$result = curl_exec($curl);
curl_close($curl);
print htmlspecialchars($result);
?>
这个网站(ball-control.com)阻止了卷曲功能。但我需要它。
这是结果
<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
</head><body><form method="POST" id="super">
<script>document.getElementById('super').style.display="none";</script>
<input type="hidden" value="1" name="wlsec"><center>
<br>Чтобы продолжить работу с сайтом, пожалуйста, нажмите кнопку «Продолжить».<br><br>
<input value="Продолжить" type="submit"></center>
</form><script>document.getElementById('super').submit()</script>
</body></html>
这个结果是错误的,我需要真正的网站内容。我怎样才能抓取或获取本网站的内容? PLZ帮助。 TNX
答案 0 :(得分:1)
只是分析您发布的结果,他们正试图避免使用隐藏形式的机器人
<?php
$url = "http://ball-control.com/";
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER=>true,
CURLOPT_URL=>$url,
CURLOPT_FOLLOWLOCATION=>true,
CURLOPT_TIMEOUT=>10,
CURLOPT_CONNECTTIMEOUT=>60,
CURLOPT_COOKIEFILE=>"",
CURLOPT_POST=>true,
CURLOPT_POSTFIELDS=>["wlsec"=>"1"]]);
$result = curl_exec($ch);
curl_close($ch);
print htmlspecialchars($result);
?>
您可以看到隐藏的帖子字段是wlsec,必须返回1
答案 1 :(得分:1)
您的cURL
请求中缺少一些内容,主要与未设置 CURLOPT_POSTFIELDS
, CURLOPT_REFERER
,<强> CURLOPT_COOKIEFILE
即可。
页面上有一个隐藏的表单,需要wlsec
提交值为1
的{{1}}。之后,站点检查以查看引用者是什么,最后验证cookie和php会话ID。
$formdata = array('wlsec' => '1');
$url = 'http://ball-control.com';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_REFERER, $url);
curl_setopt($curl, CURLOPT_COOKIEFILE, '/tmp/cookies.txt'); // set cookie file
curl_setopt($curl, CURLOPT_COOKIEJAR, '/tmp/cookies.txt'); // same file as above
将以下内容添加到您已有的中,可以解决问题。