0

我正在使用 PHP curl 方法构建一个网络抓取工具,以从 ASPX 页面抓取结果。我成功地抓取了第一个结果页面,但是当我尝试从后续页面获取结果时出现问题,但出现运行时错误。

我查看了所有隐藏字段,只找到了 __VIEWSTATE 和 __EVENTVALIDATION 并更新了这些值。我不确定为什么会收到此运行时错误。

$url = "";

$results_page = scrape($url,'',"","");

preg_match('~<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="(.*?)" />~', $results_page, $viewstate);

preg_match('~<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="(.*?)" />~', $results_page, $eventValidation);

$viewstate = $viewstate[1];
$eventValidation = $eventValidation[1];

echo $results_page;
sleep(rand(3,5)); 

 // get next results page 
echo $results_page = scrape($url,'',$viewstate,$eventValidation);



function scrape($url,$zip,$v1,$v2) {
$ckfile = tempnam("/tmp", "CURLCOOKIE");
$useragent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.2 (KHTML, like Gecko) Chrome/5.0.342.3 Safari/533.2';
$f = fopen('log.txt', 'w'); // file to write request header for debug purpose

/**    Get __VIEWSTATE & __EVENTVALIDATION */
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
$html = curl_exec($ch);
curl_close($ch);
if(!$v1){
preg_match('~<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="(.*?)" />~', $html, $viewstate);
preg_match('~<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="(.*?)" />~', $html, $eventValidation);
$viewstate = $viewstate[1];
$eventValidation = $eventValidation[1];
}
/** Start form submit process */
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_STDERR, $f);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);

// Collecting all POST fields
$postfields = array();
$postfields['__EVENTTARGET'] = "";
$postfields['__EVENTARGUMENT'] = "";
if($v1){$viewstate = $v1;$eventValidation=$v2;}
$postfields['__VIEWSTATE'] = $viewstate;
$postfields['__EVENTVALIDATION'] = $eventValidation;
$postfields['zip'] = $zip;
$postfields['county'] = '';
$postfields['POISearch'] = "agent";
if($v1){
$postfields['method'] = "query";
$postfields['pager'] = "nextData";
$postfields['searchType'] = "agent";}
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ret = curl_exec($ch);
curl_close($ch); 
return $ret;
}
4

0 回答 0