网络编程

一、get请求使用方法--固定格式

import  requests
url = 'http://api.nnzhp.cn/api/user/stu_info'
req=requests.get(url,params={'stu_name':'MLing'})
print('这是json格式',req.json())#返回内容按字典格式输出
print('这是text格式',req.text) #返回内容按test格式输出,原始返回什么就是什么
print('这是状态码',req.status_code) #请求结果中的状态
print('获取内容',req.content) #返回去请求内容,不过是字节格式的,返回文件、图片的时候使用


运行结果:
这是json格式 {'error_code': 0, 'stu_info': [{'sex': '女', 'name': 'MLing', 'gold': 566, 'phone': '13212532910', 'id': 100010878, 'age': 28, 'addr': '河南省济源市北海大道32号', 'grade': '天蝎座'}]}
这是text格式 {
        "error_code": 0,
        "stu_info": [
                {
                        "id": 100010878,
                        "name": "MLing",
                        "sex": "女",
                        "age": 28,
                        "addr": "河南省济源市北海大道32号",
                        "grade": "天蝎座",
                        "phone": "13212532910",
                        "gold": 566
                }
        ]
}
这是状态码 200
获取内容 b'{\n        "error_code": 0,\n        "stu_info": [\n                {\n                        "id": 100010878,\n                        "name": "MLing",\n                        "sex": "\xe5\xa5\xb3",\n                        "age": 28,\n                        "addr": "\xe6\xb2\xb3\xe5\x8d\x97\xe7\x9c\x81\xe6\xb5\x8e\xe6\xba\x90\xe5\xb8\x82\xe5\x8c\x97\xe6\xb5\xb7\xe5\xa4\xa7\xe9\x81\x9332\xe5\x8f\xb7",\n                        "grade": "\xe5\xa4\xa9\xe8\x9d\x8e\xe5\xba\xa7",\n                        "phone": "13212532910",\n                        "gold": 566\n                }\n        ]\n}'

 二、post请求使用方法

 

# post请求post data  --固定格式
import  requests
url='http://api.nnzhp.cn/api/user/login'
req=requests.post(url,data={'username':'niuhanyang','passwd':'***'}) #传参格式固定
print(req.json())
#qq群
import  requests
url='https://qun.qq.com/cgi-bin/qun_mgr/get_group_list'
data={'bkn':208992859}
req=requests.post(url,data)
print(req.json())

运行结果:
{'error_code': 0, 'login_info': {'login_time': '20200527102730', 'sign': 'eb5c2eb9af952aab941702a480b45369', 'userId': 47749}}
{'em': ' [errcode:4:0]', 'errcode': 0, 'ec': 4}

  三、发送header、cookie 

#cookies
url="https://qun.qq.com/cgi-bin/qun_mgr/get_group_list"
data ={"bkn": 208992859}
d = {'pgv_pvi': '6636933120', 'RK': 'gRZhhBpNbS', 'ptcz': '14bab564718e3e1048a09cc0e18a23f7c51f20d5b93da610cc1427f51f63a2f8', 'pgv_pvid': '4990195883', 'ts_uid': '5190463916', 'uin': 'o0511402865', 'pgv_si': 's7505852416', 'skey': '@2ttDS8Ljw', 'p_uin': 'o0511402865', 'pt4_token': 'AgqIsYBhO1b82zx1N4SxoGpCxGV0d38ss7jCI1nYfIg_', 'p_skey': '9nlMjw4Uy44*Hu5iL3DOFonmAtZtExiniLykrsIRKmM_', 'traceid': '14035c8a79'}
req = requests.post(url,data,cookies=d)
print(req.json())

 注意:这种方式需要先将cookies转为字典

url="https://qun.qq.com/cgi-bin/qun_mgr/get_group_list"
data ={"bkn": 208992859}
header = {'cookie':"pgv_pvi=6636933120; RK=gRZhhBpNbS; ptcz=14bab564718e3e1048a09cc0e18a23f7c51f20d5b93da610cc1427f51f63a2f8; pgv_pvid=4990195883; ts_uid=5190463916; uin=o0511402865; pgv_si=s7505852416; skey=@2ttDS8Ljw; p_uin=o0511402865; pt4_token=AgqIsYBhO1b82zx1N4SxoGpCxGV0d38ss7jCI1nYfIg_; p_skey=9nlMjw4Uy44*Hu5iL3DOFonmAtZtExiniLykrsIRKmM_; traceid=14035c8a79"}
req = requests.post(url,data,headers=header)
print(req.json())

  四、文件下载

import  requests
url='https://q4.qlogo.cn/g?b=qq&nk=1561131041&s=140'
req=requests.get(url)
f=open('MLing.jpg','wb')
f.write(req.content)
f.close()

  五、文件上传

import  requests
url='http://api.nnzhp.cn/api/file/file_upload'
f=open('MLing.jpg','rb')
data={'file':f}
req=requests.post(url,files=data)
print(req.json())
f.close()

  六、json格式入参

#传json格式入参--钉钉机器人
import  requests
url='https://oapi.dingtalk.com/robot/send?access_token=44402c9408df8cf3f429c02a20399fc34604f98cf572fcaeaa3f9592426176a7'
d={"msgtype": "text","text": {"content": "下课了!besttest!"}}
# d为把hd_cookie转为字典
req=requests.post(url,json=d)
print(req.json())
print(req.cookies) #获取cookies

  

 

posted @ 2020-05-27 11:05  MLing  阅读(225)  评论(0编辑  收藏  举报