Python Interface Automation Test (i)-request module

Source: Internet
Author: User

The Urllib.request module is python3 for handling URLs.

1. First Import:

From Urllib Import Request

2. Construct the URL, construct the URL's headers information and send the reference "request." Request "

----GET request with no parameters "slightly"

----GET request with parameters "with header, with data, with header and data, with pre-issued host"

Import: from Urllib Import parse

Import JSON

url = r "xxxxx" NOTE: If you configure host, the host domain name in the address will be rewritten as the host address "

headers = {' Host ': ' Domain name ',

' Cookie ' = ' xxxx ',

}

data = {' CANSHU1 ' = AAA,

' Canshu2 ' = BBB,

' Canshu3 ' = CCC,

' Canshu4 ' = ddd

}

data = Parse.urlencode (data). Encode (' Utf-8 ')

req = Request. Request (Url,headers=headers,data=data)

3. Request the URL "Request.urlopen" and get the return parameter ". Read ()"

page = Request.urlopen (req). Read

page = Page.decode (utf-8)

4. Convert the returned JSON format to Python's data Structure "json.loads (XX)"

Page_python = json.loads (page)

5. Parse the returned data type and loop through the desired content multiple times

--Print data type: print (Type (a))

--Direct Fetch dictionary value value: page_python[' key value 1 ' [' Key value 2 '] [' key value 3 ']

Python Interface Automation Test (i)-request module

Related Article

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.