import requests

1、get请求
url = "http://sky.nnzhp.cn/api/sparrow/student"
data = {"limit":2}
response = requests.get(url,data)#发get请求
#获取结果
print(response.json()) #返回的是一个字典,如果返回的json不合法就会报错
print(response.text()) #返回的是字符串
print(response.content()) #返回的是一个bytes类型
print(response.cookies()) #返回的是cookies
print(response.status_code()) #返回的是http的状态码
print(response.url()) #返回的是请求的url
print(response.headers()) #返回的是请求头
2、post请求
url = "http://sky.nnzhp.cn/api/user/register"
data = {"phone":18612532834,"email":"qqqq1@qq.com",
        "nick":'小白',"password":"123456","password2":"123456"}
response = requests.post(url,data=data)  #发post请求,如果参数是在url里面要用params这个参数,k-v格式

print(response.json()) #返回的是一个字典,如果返回的json不合法就会报错




3、cookies请求 cookies="cookies  方式1  少的建议
url = "http://sky.nnzhp.cn/api/user/user_info"
headers= {"cookies":"srgregregerg"}
response = requests.get(url,cookies="cookies")
url = "https://qun.qq.com/cgi-bin/qun_mgr/search_group_members"
data={
    'gc': 117545216,
    'st': "0",
    'end': "20",
    'sort': 0,
    'bkn': "1748190229"
}
cookie={'_qpsvr_localtk': '0.9708018590285696',
        'uin': 'o1164019076', 'skey': '@21AFqIqD7',
        'RK': 'h6RBy2WqQM', 'ptcz': '20a23ea6b2deb099287624b4e452bb0a9419dc427617d8344aa79fae1417d6eb',
        'p_uin': 'o1164019076', 'pt4_token': 'CmdQpCaCkY4OjNWktzbggxGQhO*FxtHJyqADQG3Gjag_',
        'p_skey': 'nJY4KF-Dr2OFb*2dtAwiNjtW-AJ6Z48ADb6q4ccdQys_',
        'traceid': '4b3d0e4d8f'}
req=requests.post(url,data,cookies=cookie)
headers  方式2 多的建议
url = "http://sky.nnzhp.cn/api/user/user_info"
headers= {"token":"srgregregerg"}
response = requests.get(url,headers=headers) #需要传headers,header={cookie:""}
url = "https://qun.qq.com/cgi-bin/qun_mgr/search_group_members"
data={
    'gc': 117545216,
    'st': "0",
    'end': "20",
    'sort': 0,
    'bkn': "1748190229"
}
header={"cookie": "_qpsvr_localtk=0.9708018590285696; uin=o1164019076; skey=@21AFqIqD7; RK=h6RBy2WqQM; ptcz=20a23ea6b2deb099287624b4e452bb0a9419dc427617d8344aa79fae1417d6eb; p_uin=o1164019076; pt4_token=CmdQpCaCkY4OjNWktzbggxGQhO*FxtHJyqADQG3Gjag_; p_skey=nJY4KF-Dr2OFb*2dtAwiNjtW-AJ6Z48ADb6q4ccdQys_; traceid=4b3d0e4d8f"}
req=requests.post(url,data,headers=header)
4、json请求 json=date
url = "http://sky.nnzhp.cn/api/user/register"
date={
      'xxx';'xxxx'
}
response = requests.json(url,json=date)
print(response.json)
print(response.url)


5、上传文件
url = "http://sky.nnzhp.cn/api/user/register"
data = {"phone":18612532834,"email":"qqqq1@qq.com",
        "nick":'小灰',"password":"123456","password2":"123456"}
file = {"avatar":open("ssss.jpg","rb")}#读模式,加个字典后边是文件对象
response = requests.post(url,data=data,files=file)

#注册完用这个登录:http://sky.nnzhp.cn/


6、下载文件
url = "http://sky.nnzhp.cn/static/avatar/default.jpg"
response = requests.get(url) #获取结果
f = open("xiaozhu.jpg","wb")#打开文件,模式
f.write(response.content)#返回的by类型,结果写进来
f.close()