302 redirection is displayed for crawling website data!

Source: Internet
Author: User
302 redirection is displayed for crawling website data! Target website: http://www.dltm.net/webtmq/free/free_query.php
Step 1: Enter the trademark number and submit it (post)

Step 2: Click the trademark number

Step 3: Data to be captured


The first two steps have been captured, but the last step is always redirected to 302, leading to data capture failure.
Related code:
// Step 1: 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); // the returned results are stored in the variable, instead of directly outputting curl_setopt ($ ch, CURLOPT_HEADER by default, 0); curl_setopt ($ ch, CURLOPT_REFERER, REFFER_URL); $ result = curl_exec ($ ch); // Save the output page to $ result in curl_close ($ ch ); preg_match_all ('', $ Result, $ rs); // Step 2 $ 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' => '000000', '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); // the returned results are stored in the variable, instead of outputting curl_setopt ($ ch, CURLOPT_HEADER, 0) by default; curl_setopt ($ ch, CURLOPT_REFERER, REFFER_URL1); curl_setopt ($ ch, CURLOPT_POST, 1 ); // submit curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ fields_string) in POST mode; $ result = curl_exec ($ ch ); // Save the output page to $ result in curl_close ($ ch); // Step 3, 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); // the returned results are stored in the variable, instead of outputting curl_setopt ($ ch, CURLOPT_HEADER, 0) by default; curl_setopt ($ ch, CURLOPT_REFERER, REFFER_URL1); $ result = curl_exec ($ ch ); // Save the output page to $ result in curl_close ($ ch); print_r ($ result); exit; // no data is obtained at this step.

You can copy the code and run it on your own. The data in step 3 is not available. could you help me to see it? if you can get the data, post your source code? thank you very much !!!


Reply to discussion (solution)

Add
Curl_setopt ($ ch, CURLOPT_COOKIEJAR, "cookie.txt ");
Curl_setopt ($ ch, CURLOPT_COOKIEFILE, "cookie.txt ");

Add
Curl_setopt ($ ch, CURLOPT_COOKIEJAR, "cookie.txt ");
Curl_setopt ($ ch, CURLOPT_COOKIEFILE, "cookie.txt ");

No. print 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 )

Why not?
Take registration number 8437927 as an example
Create a new file cookie.txt and run the code

$ Cookie = realpath('cookie.txt '); // This is the added // Step 1 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 ); // the returned results are stored in the variable instead of the default direct output curl_setopt ($ ch, CURLOPT_HEADER, 0); curl_setopt ($ ch, CURLOPT_REFERER, REFFER_URL); curl_setopt ($ ch, CURLOPT_COOKIEFILE, $ cookie); // This is the added curl_setopt ($ ch, CURLOPT_COOKIEJAR, $ cookie); // This is the added $ result = curl_exec ($ ch ); // Save the output page to $ result in curl_close ($ ch); preg_match_all ('', $ Result, $ rs); // Step 2 $ 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' => '000000', '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); // the returned results are stored in the variable, instead of outputting curl_setopt ($ ch, CURLOPT_HEADER, 0) by default; curl_setopt ($ ch, CURLOPT_REFERER, REFFER_URL1); curl_setopt ($ ch, CURLOPT_POST, 1 ); // submit curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ fields_string) in POST mode; curl_setopt ($ ch, CURLOPT_COOKIEFILE, $ cookie); // This is the added curl_setopt ($ ch, CURLOPT_COOKIEJAR, $ cookie); // This is the added $ result = curl_exec ($ ch); // Save the output page to $ result in curl_close ($ ch ); // Step 3, 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); // the returned results are stored in the variable, instead of outputting curl_setopt ($ ch, CURLOPT_HEADER, 0) by default; curl_setopt ($ ch, CURLOPT_REFERER, REFFER_URL1); curl_setopt ($ ch, CURLOPT_COOKIEFILE, $ cookie ); // This is the added curl_setopt ($ ch, CURLOPT_COOKIEJAR, $ cookie); // This is the added $ result = curl_exec ($ ch ); // Save the output page to $ result in curl_close ($ ch); print_r ($ result); exit;

Why not?
Take registration number 8437927 as an example
Create a new file cookie.txt and run the code

$ Cookie = realpath('cookie.txt '); // This is the added // Step 1 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 ); // the returned results are stored in the variable instead of the default direct output curl_setopt ($ ch, CURLOPT_HEADER, 0); curl_setopt ($ ch, CURLOPT_REFERER, REFFER_URL); curl_setopt ($ ch, CURLOPT_COOKIEFILE, $ cookie); // This is the added curl_setopt ($ ch, CURLOPT_COOKIEJAR, $ cookie); // This is the added $ result = curl_exec ($ ch ); // Save the output page to $ result in curl_close ($ ch); preg_match_all ('', $ Result, $ rs); // Step 2 $ 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' => '000000', '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); // the returned results are stored in the variable, instead of outputting curl_setopt ($ ch, CURLOPT_HEADER, 0) by default; curl_setopt ($ ch, CURLOPT_REFERER, REFFER_URL1); curl_setopt ($ ch, CURLOPT_POST, 1 ); // submit curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ fields_string) in POST mode; curl_setopt ($ ch, CURLOPT_COOKIEFILE, $ cookie); // This is the added curl_setopt ($ ch, CURLOPT_COOKIEJAR, $ cookie); // This is the added $ result = curl_exec ($ ch); // Save the output page to $ result in curl_close ($ ch ); // Step 3, 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); // the returned results are stored in the variable, instead of outputting curl_setopt ($ ch, CURLOPT_HEADER, 0) by default; curl_setopt ($ ch, CURLOPT_REFERER, REFFER_URL1); curl_setopt ($ ch, CURLOPT_COOKIEFILE, $ cookie ); // This is the added curl_setopt ($ ch, CURLOPT_COOKIEJAR, $ cookie); // This is the added $ result = curl_exec ($ ch ); // Save the output page to $ result in curl_close ($ ch); print_r ($ result); exit;


Thank you. I just got in touch with curl. The cookie location is missing.
The problem has been solved. paste it !!!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.