网络请求request day07-11

# 1.post
# 2.get
# 3.url里面传参
# 4.body json
# 5.cookie
# 6.header
# 7.k -v
# 8.上传文件

import requests

# get
# url = "http://api.nnzhp.cn/api/user/stu_info"
# data = {"stu_name": "小"}
# res = requests.get(url, data)

# post
# url = "http://api.nnzhp.cn/api/user/login"
# data = {"username": "niuhanyang", "passwd": "aA123456"}
# data2 = {"version": 1}
# # 如果post 参数在URL里面 就写在params
# # 如果参数是在body里面 就写在data里
# res = requests.post(url, params=data2, data=data)
# print(res.url) # 查看请求发出去的URL


# # json
# url = "http://api.nnzhp.cn/api/user/add_stu"
# data = {
# "grade":"fmzz",
# "phone":"12345608991",
# "name":"你猜"
# }
# res = requests.post(url,json=data)

# cookie
# url = "https://qun.qq.com/cgi-bin/qun_mgr/search_group_members"
# data = {
# "gc": "7501017",
# "st": "0",
# "end": "20",
# "sort": 0,
# "bkn": 53024039
# }
# cookie = {"cookie": "RK=vswpWpGmTk; ptcz=bd1799a527a599bc9ac29a7754005b0b0cc4861f99dc2ebd8ad761e11e495851; "
# "pgv_pvid=7249439792; pgv_pvi=1512488960; tvfe_boss_uuid=0fa9ae31c8683d6d; o_cookie=1195665307; "
# "pac_uid=1_1195665307; eas_sid=L1b6y1l5A848p1O5w1U4n0N5y6; "
# "__guid=14431543.4441650296924942000.1618218798379.9321; "
# "BIDUPSID=BCA0C1388E2472ADF8CABB90D8440461; PSTM=1618304933; "
# "BAIDUID=89BCC6DF2C355C4FA2DB4DEE79781652:FG=1; iip=0; traceid=4b7848e787; "
# "_qpsvr_localtk=0.5216509271880581; uin=o1195665307; skey=@hULH9H6Ln; p_uin=o1195665307; "
# "pt4_token=lz60j2a3Vf-SBesAtJsRa8PVlENmw0L-6LHigCoLJAY_; "
# "p_skey=Gfbliy1ruJfkeTgZ-boOejCqGM1iQhYsftVulkX3r4g_; monitor_count=5"}
# res = requests.post(url, data,cookies = cookie)

# header
# url = "https://qun.qq.com/cgi-bin/qun_mgr/search_group_members"
# data = {
# "gc": "7501017",
# "st": "0",
# "end": "20",
# "sort": 0,
# "bkn": 53024039
# }
# header = {"cookie": "RK=vswpWpGmTk; ptcz=bd1799a527a599bc9ac29a7754005b0b0cc4861f99dc2ebd8ad761e11e495851; "
# "pgv_pvid=7249439792; pgv_pvi=1512488960; tvfe_boss_uuid=0fa9ae31c8683d6d; o_cookie=1195665307; "
# "pac_uid=1_1195665307; eas_sid=L1b6y1l5A848p1O5w1U4n0N5y6; "
# "__guid=14431543.4441650296924942000.1618218798379.9321; "
# "BIDUPSID=BCA0C1388E2472ADF8CABB90D8440461; PSTM=1618304933; "
# "BAIDUID=89BCC6DF2C355C4FA2DB4DEE79781652:FG=1; iip=0; traceid=4b7848e787; "
# "_qpsvr_localtk=0.5216509271880581; uin=o1195665307; skey=@hULH9H6Ln; p_uin=o1195665307; "
# "pt4_token=lz60j2a3Vf-SBesAtJsRa8PVlENmw0L-6LHigCoLJAY_; "
# "p_skey=Gfbliy1ruJfkeTgZ-boOejCqGM1iQhYsftVulkX3r4g_; monitor_count=5"}
# res = requests.post(url, data,headers = header)

# # 上传文件
# url = "http://api.nnzhp.cn/api/file/file_upload"
# data = {"file":open("test.xls","rb")}
# res = requests.post(url,files =data )

# 下载文件
url = "https://q4.qlogo.cn/g?b=qq&nk=175177730&s=140"
# 如果是https报错的话加verify=False
req = requests.get(url,verify=True)
with open("a..jpg","wb") as fw:
fw.write(req.content)

# print(res.json()) # 返回字典 如果接口返回的不是json 那会报错
# print(res.content) # 返回的是bytes类型 下载文件的时候用它
# print(res.text) # 返回字符串
# print(res.status_code) # 返回状态码
# print(res.cookies) # 返回的cookie
# print(res.headers) # 返回header
上一篇:day07_10 类中的特殊方法


下一篇:网络请求-抓取qq群好友信息 day07-10