Python发送get、post请求

import json
import requests
#获取北京天气
#
#url= "https://wis.qq.com/weather/common?source=xw&weather_type=forecast_1h|forecast_24h|index|alarm|limit|tips&province=北京&city=北京&county=昌平区"
#req = requests.get(url)# 发送请求
#print(req.text)# 得到的是json格式
#print(req.json)# 得到的是字典格式
#print(type(req.text))
#print(type(req.json))

#发送post请求,登录并将个人信息保存到本地
url = "https://api.apiopen.top/developerLogin"
data = {'name':'xxx','passwd':'xxx'}
req = requests.post(url,data)#发送post请求,第一个参数是URL,第二个参数是请求数据
filename = 'D:/write_data.txt'
with open(filename,'w') as f:
    f.write(str(req.json())+"
") 
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())

# 添加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:esttest	epython-mppday7练习11.jpg','rb')
# r = requests.post(url,files={'file':f})
# users_dic = r.json()
# print(users_dic)

# 下载文件
# 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()

#把浏览器页面下载到本地   保存网页,可以理解为简单的爬虫工具
#url='http://www.nnzhp.cn/archives/630'
#r = requests.get(url)
#f = open('nnzhp.html','wb')
#f.write(r.content)
#f.close()

  

原文地址:https://www.cnblogs.com/c-jw/p/12571310.html