目標網站:http://www.dltm.net/webtmq/free/free_query.php
第一步:輸入商標號,提交(post抓起)
第二步:點擊商標號
第三步:需要抓取的資料
前兩步均已實現抓取,但抓取最後一步時老是出現302重新導向,導致資料抓取不到。
相關代碼:
//第一步define(TARGET_URL,'http://www.dltm.net/webtmq/free/free_query.php');define(REFFER_URL,'http://www.dltm.net');$url=TARGET_URL;$ch=curl_init($url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//返回結果存放在變數中,而不是預設的直接輸出curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_REFERER, REFFER_URL);$result= curl_exec($ch);//儲存輸出的頁面到$result中curl_close($ch);preg_match_all('',$result,$rs);//第二步$fields_post = array('ip'=>$rs[1][0],'textarea_explain'=>'%B2%E9%D1%AF%C8%AB%B2%BF%C0%E0%B1%F0','tm_lb'=> '0','tm_key'=>'8437927','tm_key_item'=>'tm_zch','query_mode'=>'1');$fields_string='';foreach($fields_post as $key => $value){ $fields_string .= $key . '=' . $value . '&';}$fields_string = rtrim($fields_string,'&');define(TARGET_URL1,'http://www.dltm.net/webtmq/free/free_res.php');define(REFFER_URL1,'http://www.dltm.net/webtmq/free/free_res.php');$url=TARGET_URL1;$ch=curl_init($url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//返回結果存放在變數中,而不是預設的直接輸出curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_REFERER, REFFER_URL1);curl_setopt($ch,CURLOPT_POST,1);//以POST方式提交curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);$result= curl_exec($ch);//儲存輸出的頁面到$result中curl_close($ch);//第三步preg_match_all('',$result,$res);$url = 'http://www.dltm.net/webtmq/free/'.$res[1][0];$ch=curl_init($url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//返回結果存放在變數中,而不是預設的直接輸出curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_REFERER, REFFER_URL1);$result= curl_exec($ch);//儲存輸出的頁面到$result中curl_close($ch);print_r($result);exit;//這一步得不到資料
可以自己copy一下代碼運行一下,第三步資料得不到,大神能幫我看看麼,如果能得到資料,請貼出你的源碼,非常感謝!!!
回複討論(解決方案)
增加
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
增加
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
不行啊,列印curl_getinfo($ch)
Array ( [url] => http://www.dltm.net/webtmq/free/free_detail.php?ip=7f6179b25de2974b0e0b33d01c2fef18&class_id=25&id=10309774 [content_type] => text/html [http_code] => 302 [header_size] => 440 [request_size] => 193 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.094 [namelookup_time] => 0 [connect_time] => 0.047 [pretransfer_time] => 0.047 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => 0 [starttransfer_time] => 0.094 [redirect_time] => 0 [certinfo] => Array ( ) [redirect_url] => http://www.dltm.net/webtmq/free/free_query.php )
怎麼不可以呢?
以註冊號 8437927 為例
建立一檔案 cookie.txt,執行代碼
$cookie = realpath('cookie.txt'); //這是增加的//第一步define('TARGET_URL','http://www.dltm.net/webtmq/free/free_query.php');define('REFFER_URL','http://www.dltm.net');$url=TARGET_URL;$ch=curl_init($url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//返回結果存放在變數中,而不是預設的直接輸出curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_REFERER, REFFER_URL);curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); //這是增加的curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); //這是增加的$result= curl_exec($ch);//儲存輸出的頁面到$result中curl_close($ch);preg_match_all('',$result,$rs); //第二步$fields_post = array( 'ip'=>$rs[1][0], 'textarea_explain'=>'%B2%E9%D1%AF%C8%AB%B2%BF%C0%E0%B1%F0', 'tm_lb'=> '0', 'tm_key'=>'8437927', 'tm_key_item'=>'tm_zch', 'query_mode'=>'1'); $fields_string='';foreach($fields_post as $key => $value){ $fields_string .= $key . '=' . $value . '&';}$fields_string = rtrim($fields_string,'&'); define('TARGET_URL1','http://www.dltm.net/webtmq/free/free_res.php');define('REFFER_URL1','http://www.dltm.net/webtmq/free/free_res.php');$url=TARGET_URL1;$ch=curl_init($url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//返回結果存放在變數中,而不是預設的直接輸出curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_REFERER, REFFER_URL1);curl_setopt($ch,CURLOPT_POST,1);//以POST方式提交curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); //這是增加的curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); //這是增加的$result= curl_exec($ch);//儲存輸出的頁面到$result中curl_close($ch); //第三步preg_match_all('',$result,$res);$url = 'http://www.dltm.net/webtmq/free/'.$res[1][0]; $ch=curl_init($url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//返回結果存放在變數中,而不是預設的直接輸出curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_REFERER, REFFER_URL1);curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); //這是增加的curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); //這是增加的$result= curl_exec($ch);//儲存輸出的頁面到$result中curl_close($ch);print_r($result);exit;
怎麼不可以呢?
以註冊號 8437927 為例
建立一檔案 cookie.txt,執行代碼
$cookie = realpath('cookie.txt'); //這是增加的//第一步define('TARGET_URL','http://www.dltm.net/webtmq/free/free_query.php');define('REFFER_URL','http://www.dltm.net');$url=TARGET_URL;$ch=curl_init($url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//返回結果存放在變數中,而不是預設的直接輸出curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_REFERER, REFFER_URL);curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); //這是增加的curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); //這是增加的$result= curl_exec($ch);//儲存輸出的頁面到$result中curl_close($ch);preg_match_all('',$result,$rs); //第二步$fields_post = array( 'ip'=>$rs[1][0], 'textarea_explain'=>'%B2%E9%D1%AF%C8%AB%B2%BF%C0%E0%B1%F0', 'tm_lb'=> '0', 'tm_key'=>'8437927', 'tm_key_item'=>'tm_zch', 'query_mode'=>'1'); $fields_string='';foreach($fields_post as $key => $value){ $fields_string .= $key . '=' . $value . '&';}$fields_string = rtrim($fields_string,'&'); define('TARGET_URL1','http://www.dltm.net/webtmq/free/free_res.php');define('REFFER_URL1','http://www.dltm.net/webtmq/free/free_res.php');$url=TARGET_URL1;$ch=curl_init($url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//返回結果存放在變數中,而不是預設的直接輸出curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_REFERER, REFFER_URL1);curl_setopt($ch,CURLOPT_POST,1);//以POST方式提交curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); //這是增加的curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); //這是增加的$result= curl_exec($ch);//儲存輸出的頁面到$result中curl_close($ch); //第三步preg_match_all('',$result,$res);$url = 'http://www.dltm.net/webtmq/free/'.$res[1][0]; $ch=curl_init($url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//返回結果存放在變數中,而不是預設的直接輸出curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_REFERER, REFFER_URL1);curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); //這是增加的curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); //這是增加的$result= curl_exec($ch);//儲存輸出的頁面到$result中curl_close($ch);print_r($result);exit;
謝謝,剛接觸curl,cookie位置少放了
問題已解決,結貼!!!!