Urllib2 redirect/cookie Processing Method

Source: Internet
Author: User
Urllib2 provides a wide range of URL-based resource processing methods ~ You can use handler to implement various functions ~ Likewise, automatic redirect and cookie analysis and acquisition are implemented based on status code (redirect Based on HTTP status code is also implemented in urllib. fancyurlopener ~)

The step-by-step code is as follows:

  1. Import urllib2 as ul2, cookielib as Cl, urllib as UL
  2. Cj = Cl. cookiejar ()
  3. Opener = ul2.build _ opener (ul2.httpcookieprocessor (CJ) # Here you can add more handlers
  4. # Ul2.install _ opener (opener) # You can register as the default opener. In this way, you can use urlopen/urlretrieve and other shortcuts when sending requests later.
  5. # Create and send a request
  6. Req_sohu = urllib2.request ('HTTP: // www.sohu.com ') # Use the request object to send a POST request and provide more parameters. If a GET request does not require complicated settings, can be used directly
  7. Res_sohu = opener. Open (req_sohu)
  8. Content_sohu = res_sohu.read () # Read all response content
  9. # The corresponding HTTP request and Response Headers are in req_sohu and res_sohu, which can be accessed and obtained directly.
  10. # Obtain cookies ...... Very troublesome ...... I wonder if there is any easy way ......
  11. Cookies = filter (lambda H: isinstance (H, ul2.httpcookieprocessor), opener. handlers) [0]. cookiejar

For more information about cookies, see cookielib. cookiejar.

This cookie exists throughout the entire opener lifecycle and automatically analyzes the validity period, domain, path, and maintenance. It is very suitable for simulating logon and capturing ~

------------

The title is redirect ...... Not reflected in the Code ~

In row 5th, when creating opener, ul2.httpredirecthandler is one of the handler added by default ......
Handler is also the default handler:

Proxyhandler
Unknownhandler
Httphandler
Httpdefaulterrorhandler
Httpredirecthandler
Ftphandler
Filehandler
Httpshandler
Httperrorprocessor

Wait for you ~

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.