Python Interface Automation 6-redirect (location) "Reprint"

Source: Internet
Author: User

From the blog: Shanghai-leisurely

Original address: http://www.cnblogs.com/yoyoketang/tag/python%E6%8E%A5%E5%8F%A3%E8%87%AA%E5%8A%A8%E5%8C%96/

Objective

A cock silk man a summon up the courage to the goddess B call vindicate, Goddess B is a scheming to think that the cock silk man A is a good person, do not want to directly refuse to set the call transfer to the Girlfriends C, the final cock silk male A and the goddess Girlfriends C success, this scene is actually redirected.

First, redirect

1. (Redirect) is a variety of methods to redirect various network requests to another location, from address A to address B.

2. REDIRECT Status code:

--301 redirect:301 Representative Permanent transfer (permanently Moved)

--302 redirect:302 represents a temporary transfer (temporarily Moved)

3. For a simple case, log in to the blog to open my blog first page, into my essay editing interface, remember this address: https://i.cnblogs.com/EditPosts.aspx?opt=1

4. Exit the Blog Park login, put just my essay this address input browser Enter, grabbed the package will see this request status code is 302, the browser address bar instant Refresh jump to login home go

Ii. Prohibition of redirection (allow_redirects)

1. Request with Get method: Https://i.cnblogs.com/EditPosts.aspx?opt=1

2. The Print status code is 200 because the Requets library automatically handles the redirect request.

3. After the redirect address is automatically processed, we will not get the redirected URL, we cannot go to the next step, here we can set a parameter prohibit redirection:allow_ Redirects=False

(allow_redirects=true is the start redirect), then you can see that Status_code is 302.

Third, get redirect address

1. After the first request, the server will be issued a new request link, in the response headers, the following packet capture: location

2. Use the script to get the location address

Iv. Reference Code:

# Coding:utf-8
Import requests
# Request Header
headers = {
"User-agent": "mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) gecko/20100101 firefox/44.0 "
}
s = requests.session ()
# Open My essay.
r = S.get (' Https://i.cnblogs.com/EditPosts.aspx?opt=1 ',
Headers=headers,
Allow_redirects=true,
Verify=false)
# Print status code, automatically handle REDIRECT requests
Print R.status_code
New_url = r.headers["Location"]
Print New_url

Python Interface Automation 6-redirect (location) "Reprint"

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.