Python Automated test Learning Note -6urllib Module &request module

Source: Internet
Author: User
Tags urlencode

The Python3 Urllib module provides the ability to get a page.

urllib.request. urlopen( url, data=none, [ timeout, ] *, cafile=none, capath= None, cadefault=false, context=none)

-url: The URL that needs to be opened

-Data submitted by Data:post

-Timeout: Set the access time-out for a website

Get the page directly with the Urlopen () of the Urllib.request module, the data format of page is bytes type, need decode () decode, convert to STR type.

Import Urllib.request

# import JSON
# import Requests
Url="Http://api.nnzhp.cn/api/user/stu_info?stu_name=xiaohei"
Req=urllib.request.urlopen (URL)
Res=req.read (). Decode ()
Print (RES)

Perform:

{
"Error_code": 2,
"MSG": "No Results"
}

Implementing post data requests in Urllib

The data parameter of Urlopen () defaults to none, and when the data parameter is not empty, Urlopen () is submitted as post.

url1=' Http://api.nnzhp.cn/api/user/login '
data={
' Username ':' Niuhanyang ',
passwd ': }
#urlencode () The main function is to enclose the URL to submit the data. After the UrlEncode (), the data is converted to username=niuhanyang&passwd=aa123456,
## post must be bytes or iterable of bytes, not str, so encode () encoding is required
data=urllib.parse.urlencode (data). Encode ( ' utf-8 ')
#最终提交的url是http://api.nnzhp.cn/api/user/login?username= niuhanyang?passwd=aa123456
req=urllib.request.request (URL1, data=data)
Page=urllib.request.urlopen (req). Read ()
Span style= "COLOR: #000080" >print (Page.decode ())

To perform the view results:

{
"Error_code": 0,
"Login_info": {
"Login_time": "20180129202722",
"Sign": "7E4C46E5790CA7D5165EB32D0A895AB1",
"UserId": 1
}
}

We see the use of urllib will be more troublesome, need to transcode, assignment and other operations, request module can be more simple to complete the requested operation, as follows:

1, first need to install the request module

PIP Install requests

2. Import Request Module

Import requests

The various interface operations are as follows:

Import requests
Import JSON
#发送无参数的get请求
Url=' Http://www.baidu.com '
Req=requests.get (URL)
Print (Req.text)#返回的字符串类型

#发送有参数的request请求
url1=' Http://api.nnzhp.cn/api/user/stu_info?stu_name=feifei '
Req1=requests.get (URL1)
Print (Req1.json ())#返回的字典列表

#发送post请求
Url2=' Http://api.nnzhp.cn/api/user/login '
data={
' Username ':' Niuhanyang ',
' passwd ':' aA123456 '
}
Req=requests.post (Url2,data)#发送的post氢气, the first parameter is the URL, the second parameter is the requested data
Print (Req.json ())

#发送入参是json类型的post请求
url3=' Http://api.nnzhp.cn/api/user/add_stu '
data={
' Name ':' Feifei ',
' Phone ':' 13121111112 ',
' Grade ':' 1000 '
}

Req=requests.post (URL3,Json=data)
Print (Req.json ())

#发送带有cookie的post请求
#添加cookie
url4=' Http://api.nnzhp.cn/api/user/gold_add '
data={
' stu_id ':230,
' Gold ':88888
}
cookies={' Feifei ':' A2b454c3830e20e7d9916f6b52d6a3a7 '}
Req=requests.post (Url4,data,Cookies=cookies)
Print (Req.json ())

#发送带有Referer请求的post请求
#
url5=' Http://api.nnzhp.cn/api/user/all_stu '
data={
' Referer ':' http://api.nnzhp.cn/'
}
Req=requests.get (URL5,Headers=data)
Print (Req.json ())

#下载文件请求

url6=' Https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1517138333609&di= 327abc49fc6d63fed19124cdf826d130&imgtype=0&src=http%3a%2f%2fimg4.duitang.com%2fuploads%2fitem%2f201510 %2f17%2f20151017223821_zswbc.jpeg '
R=requests.get (URL6)#下载直接请求url然后进行保存
#print (R.status_code) #请求状态码是二进制
Res=r.content#获取二进制格式
fw=Open' Feifei.jpg ',' WB ')
Fw.write (RES)#保存文件
Fw.close ()

#上传文件

url7=' Http://api.nnzhp.cn/api/file/file_upload '
f=OpenE\\Besttest\\python\\besttest_code\\ practice \\ day7 notes \\api\\ feifei.jpg ", R=requests.post (Url7,files={ print (R.json ())
#
#下载页面
Url=' http://www.runoob.com/python/python-intro.html '
R=requests.get (URL)
f=Open (' python.html ',' WB ')
F.write (r.content)
F.close ()



























Python Automated test Learning Note -6urllib Module &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.