Python uses the request module to send post and get requests

Source: Internet
Author: User

Import requests
Import JSON

#发送get请求并得到结果
# URL = ' http://api.nnzhp.cn/api/user/stu_info?stu_name= little black Horse ' #请求接口
# req = requests.get (URL) #发送请求
# print (req.text) #获取请求, get the JSON format
# Print (Req.json ()) #获取请求, get the dictionary format
# Print (Type (req.text))
# Print (Type (Req.json ()))

#发送post请求, registering the interface
# url = ' Http://api.nnzhp.cn/api/user/user_reg '
# data = {' username ': ' mpp0130 ', ' pwd ': ' Mp123456 ', ' cpwd ': ' Mp123456 '}
# req = Requests.post (url,data) #发送post请求, the first parameter is a URL, the second parameter is the request data
# Print (Req.json ())

#入参是json
# url = ' Http://api.nnzhp.cn/api/user/add_stu '
# data = {' name ': ' Mapeipei ', ' Grade ': ' Mp123456 ', ' phone ': 15601301234}
# req = Requests.post (url,json=data)
# Print (Req.json ())

#添加header
# url = ' Http://api.nnzhp.cn/api/user/all_stu '
# header = {' Referer ': ' http://api.nnzhp.cn/'}
# res = Requests.get (url,headers=header)
# Print (Res.json ())

# Add a cookie
# url = ' Http://api.nnzhp.cn/api/user/gold_add '
# data = {' stu_id ': 231, ' Gold ': 123}
# cookie = {' Niuhanyang ': ' 7e4c46e5790ca7d5165eb32d0a895ab1 '}
# req = Requests.post (Url,data,cookies=cookie)
# Print (Req.json ())

#上传文件
# url = ' Http://api.nnzhp.cn/api/file/file_upload '
# f = open (R ' E:\besttest\te\python-mpp\day7\ practice \11.jpg ', ' RB ')
# r = requests.post (url,files={' file ': F})
# users_dic = R.json ()
# Print (users_dic)

# download file
# url = ' http://www.besttest.cn/data/upload/201710/f_36b1c59ecf3b8ff5b0acaf2ea42bafe0.jpg '
# r = Requests.get (URL)
# print (R.status_code) #获取请求的状态码
# print (r.content) #获取返回结果的二进制格式
# FW = open (' mpp.jpg ', ' WB ')
# Fw.write (r.content)
# Fw.close ()

#把浏览器页面下载到本地 save Web page, can be understood as a simple crawler tool
Url= ' http://www.nnzhp.cn/archives/630 '
r = Requests.get (URL)
f = open (' nnzhp.html ', ' WB ')
F.write (r.content)
F.close ()

Python uses the request module to send post and get requests

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.