PHP version:
<? Php <br/> $ cookie_file = fopen('cookie.txt ', 'w'); // dirname (_ file __). "/cookie _". MD5 (basename (_ file __)). ". TXT "; // set the cookie file storage path and file name </P> <p> function vlogin ($ URL, $ data) {// simulated logon to obtain the cookie function <br/> $ curl = curl_init (); // start a curl session <br/> curl_setopt ($ curl, curlopt_url, $ URL ); // address to be accessed <br/> curl_setopt ($ curl, curlopt_ssl_verifypeer, 0); // check the certificate source <br/> curl_setopt ($ curl, curlopt_ssl_verifyhost, 1); // Check whether the SSL encryption algorithm exists from the certificate <br/> curl_setopt ($ curl, curlopt_useragent, $ _ server ['HTTP _ user_agent ']); // simulate your browser <br/> curl_setopt ($ curl, curlopt_followlocation, 1); // use automatic redirect <br/> curl_setopt ($ curl, curlopt_autoreferer, 1 ); // automatically set Referer <br/> curl_setopt ($ curl, curlopt_post, 1); // send a regular POST request <br/> curl_setopt ($ curl, curlopt_postfields, $ data); // data packet submitted by post <br/> curl_setopt ($ curl, curlopt_c Ookiejar, $ globals ['cookie _ file']); // name of the file storing Cookie Information <br/> curl_setopt ($ curl, curlopt_cookiefile, $ globals ['cookie _ file']); // read the cookie information stored above <br/> curl_setopt ($ curl, curlopt_timeout, 30 ); // set the timeout limit to prevent endless loops <br/> curl_setopt ($ curl, curlopt_header, 0); // display the returned header content <br/> curl_setopt ($ curl, curlopt_returntransfer, 1); // The obtained information is returned as a file stream <br/> $ tmpinfo = curl_exec ($ curl); // perform the operation <br/> If (curl _ Errno ($ curl) {<br/> echo 'errno '. curl_error ($ curl); <br/>}< br/> curl_close ($ curl); // close the curl session <br/> return $ tmpinfo; // return data <br/>}</P> <p> function vget ($ URL) {// simulate the function for obtaining content <br/> $ curl = curl_init (); // start a curl session <br/> curl_setopt ($ curl, curlopt_url, $ URL); // address to be accessed <br/> curl_setopt ($ curl, curlopt_ssl_verifypeer, 0); // check the certificate source <br/> curl_setopt ($ curl, curlopt_ssl_verifyhost, 1); // slave Certificate <Br/> curl_setopt ($ curl, curlopt_useragent, $ _ server ['HTTP _ user_agent ']); // simulate your browser <br/> curl_setopt ($ curl, curlopt_followlocation, 1); // use automatic redirect <br/> curl_setopt ($ curl, curlopt_autoreferer, 1 ); // automatically set Referer <br/> curl_setopt ($ curl, curlopt_httpget, 1); // send a regular POST request <br/> curl_setopt ($ curl, curlopt_cookiefile, $ globals ['cookie _ file']); // read the cookie information stored above <br/> curl_seto Pt ($ curl, curlopt_timeout, 30); // set the timeout limit to prevent endless loops <br/> curl_setopt ($ curl, curlopt_header, 0 ); // display the returned header content <br/> curl_setopt ($ curl, curlopt_returntransfer, 1 ); // The obtained information is returned as a file stream <br/> $ tmpinfo = curl_exec ($ curl); // perform the operation <br/> If (curl_errno ($ curl )) {<br/> echo 'errno '. curl_error ($ curl); <br/>}< br/> curl_close ($ curl); // close the curl session <br/> return $ tmpinfo; // return data <br/>}</P> <p> function vpost ($ URL, $ data) {// simulate the data submission function <br/> $ curl = curl_init (); // start a curl session <br/> curl_setopt ($ curl, curlopt_url, $ URL); // address to be accessed <br/> curl_setopt ($ curl, curlopt_ssl_verifypeer, 0 ); // check the certificate source <br/> curl_setopt ($ curl, curlopt_ssl_verifyhost, 1 ); // check whether the SSL encryption algorithm exists from the certificate <br/> curl_setopt ($ curl, curlopt_useragent, $ _ server ['HTTP _ user_agent ']); // simulate the browser used by the user <br/> curl_setopt ($ curl, curlopt_followlocation, 1); // Use automatic redirect <br/> curl_setopt ($ curl, curlopt_autoreferer, 1); // automatically set Referer <br/> curl_setopt ($ curl, curlopt_post, 1 ); // send a regular POST request <br/> curl_setopt ($ curl, curlopt_postfields, $ data); // The data packet submitted by the post request <br/> curl_setopt ($ curl, curlopt_cookiefile, $ globals ['cookie _ file']); // read the cookie information stored above <br/> curl_setopt ($ curl, curlopt_timeout, 30 ); // set the timeout limit to prevent endless loops <br/> curl_setopt ($ curl, curlopt_header, 0); // Display the returned header content <br/> curl_setopt ($ curl, curlopt_returntransfer, 1 ); // The obtained information is returned as a file stream <br/> $ tmpinfo = curl_exec ($ curl); // perform the operation <br/> If (curl_errno ($ curl )) {<br/> echo 'errno '. curl_error ($ curl); <br/>}< br/> curl_close ($ curl); // key curl session <br/> return $ tmpinfo; // return data <br/>}</P> <p> function delcookie ($ cookie_file) {// Delete the cookie function <br/> @ unlink ($ cookie_file ); // Delete the instance <br/>}</P> <p> // use the instance <Br/> If (! File_exists ($ cookie_file) {// check whether the cookie exists <br/> vlogin ('HTTP: // example.com/login_do.asp', 'id=xxx&password=xxx '); // log on to obtain the cookie <br/>}< br/> echo vget ('HTTP: // example.com/upload_new.asp'); <br/> delcookie ($ cookie_file ); <br/>?> <Br/>
Python (1): Use urllib and cookielib
Import urllib <br/> Import urllib2 <br/> Import cookielib <br/> def xxxfunc (): <br/> "" </P> <p> "<br/> CK = cookielib. lwpcookiejar () <br/> opener = urllib2.build _ opener (urllib2.httpcookieprocessor (CK) <br/> opener. addheaders = [('user-agent', 'mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; sv1; qqdownload 528; potu (RR: 28031409: 0: 5491841 ); mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; sv1 );. net CLR 2.0.50727 ;. net CLR 1.1.4322 ;. net CLR 3.0.04506.30) ')] <br/> opener. addheaders = [('accept', '*/*')] <br/> opener. addheaders = [('Accept-language ', 'zh-cn')] <br/> opener. addheaders = [('Accept-encoding ', 'gzip, deflate')] <br/> opener. addheaders = [('connection', 'keep-alive')] <br/> opener. addheaders = [('cache-control', 'no-cache')] <br/> urllib2.install _ opener (opener) <br/> url_login = 'HTTP: // xxx.net/login' <br/> body = ('id', 'xxx'), ('PW ', 'xxx') <br/> Req = urllib. urlopen (url_login, urllib. urlencode (body) <br/> Req = urllib2.urlopen ('HTTP: // xxx.net/post ')
Python (2): Use clientcookie
Import clientcookie <br/> request = clientcookie. Request ("https://example.com /? Login & username = xxx & Password = xxx ") <br/> response = clientcookie. urlopen (request) <br/> request2 = clientcookie. request ("http://example.com/xxx") <br/> response2 = clientcookie. urlopen (request2) <br/> Print response2.read () <br/>
The above method can simulate login for general websites to achieve the expected purpose, but I am sorry that the website I want to simulate login failed, and the cookie cannot be saved.