requests 模块,可以获取网页内容;接口返回的json;向网站发起请求(get、post、cookie、header、上传文件等)。

import requests  #requests模块就是基于urllib模块开发的,所有requests模块更便宜操作

# requests.get(url).text  #text方式返回的是字符串
# res = requests.get(url).json()  #返回的json(dict或list)

#get请求
url_login = 'http://127.0.0.1:5000/login?username=admin&password=123456'
res = requests.post(url_login).json()
print(type(res),res)

#post请求
url_set = 'http://127.0.0.1:5000/set_sties'
d = {
    "stie":"牛牛杂货铺",
    "url":"http://www.nnzhp.cn/blog/"
}
res = requests.post(url_set,json=d).json()
print(res)

#添加cookie
cookie_url = 'http://127.0.0.1:5000/set_cookies'
data = {'username':'晓晓',"money":22222}
cookie = {'token':"admin-login"}
res = requests.post(cookie_url,data=data,cookies=cookie).json()
print(res)

#添加header
head_url = 'http://127.0.0.1:5000/set_header'
data = {'userid': 1}
header = {'Content-Type': "application/json"}
res = requests.post(head_url, headers=header).json()

#上传文件
up_url = 'http://127.0.0.1:5000/upload'
file = {'file_name':open('test.py')}
res = requests.post(up_url,files=file).text
print(res)

 

posted on 2017-07-07 16:19  笔-记  阅读(277)  评论(0编辑  收藏  举报