Python发送get、post请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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())+"\n")
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:\besttest\te\python-mpp\day7\练习\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()

  

posted @   橙子Cjw  阅读(339)  评论(0编辑  收藏  举报
编辑推荐:
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
阅读排行:
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Ai满嘴顺口溜,想考研?浪费我几个小时
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法
点击右上角即可分享
微信分享提示