我想从这里抓取数据:http://mediaforest.biz/mobile/nowplaying.aspx 在页面中,您选择一个电台并发布它,然后您将获得包含数据的新页面。但我无法抓住它,我再次得到同一页。
我使用了这段代码:
<?php
header ('Content-type: text/html; charset=utf-8');
$url = "http://mediaforest.biz/mobile/nowplaying.aspx";
$referer = "";
// headers
$header[] = "Host: ".parse_url($url, PHP_URL_HOST);
$header[] = "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; he; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3";
$header[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$header[] = "Accept-Language: he,en-us;q=0.7,en;q=0.3";
$header[] = "Accept-Encoding: gzip,deflate";
$header[] = "Accept-Charset: windows-1255,utf-8;q=0.7,*;q=0.7";
$header[] = "Keep-Alive: 115";
$header[] = "Connection: keep-alive";
$cookie="cookie.txt";
$fp=fopen($cookie,"w+");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_REFERER,$referer);
curl_setopt($ch, CURLOPT_TIMEOUT, 900);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER,true);
curl_setopt($ch, CURLOPT_COOKIEFILE,$cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR,$cookie);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
$content=curl_exec($ch);
echo $content;
if(stristr($content,"__EVENTTARGET")){
$array1=explode('__EVENTTARGET" value="',$content);
$content1=$array1[1];
$array2=explode('">
<input type="hidden" name="__EVENTARGUMENT"',$content1);
$content2=$array2[0];
$EVENTTARGET=urlencode($content2);
}
if(stristr($content,"__EVENTARGUMENT")){
$array1=explode('__EVENTARGUMENT" value="',$content);
$content1=$array1[1];
$array2=explode('">
<script language',$content1);
$content2=$array2[0];
$EVENTARGUMENT=urlencode($content2);
}
if(stristr($content,"formNowPlaying")){
$array1=explode('method="post" action="',$content);
$content1=$array1[1];
$array2=explode('">
<input type="hidden" name="__EVENTTARGET"',$content1);
$content2=$array2[0];
$nexturl=$content2;
}
//echo $EVENTTARGET." ".$EVENTARGUMENT." ".$nexturl;
$url = "http://mediaforest.biz/mobile/".$nexturl;
$fields = "EVENTTARGET=".$EVENTTARGET."&__EVENTARGUMENT=".$EVENTARGUMENT."&MyChannels=0&ViewChannel_Button=Show";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_REFERER,$referer);
curl_setopt($ch, CURLOPT_TIMEOUT, 900);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER,true);
curl_setopt($ch, CURLOPT_COOKIEFILE,$cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR,$cookie);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$content_stage2=curl_exec($ch);
echo $content_stage2;
?>
答案 0 :(得分:1)
如果您从第一个请求获取数据,请尝试之后关闭(针对每个请求)。
$content=curl_exec($ch);
curl_close($ch);
echo $content;
和
$content_stage2=curl_exec($ch);
curl_close($ch);
echo $content_stage2;
答案 1 :(得分:0)
我对cURL没有多少经验,我没有调查过,但乍一看似乎是这样:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
需要这样:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
虽然可能是错的。