Python教程

Python_网络请求

本文主要是介绍Python_网络请求,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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

from urllib.request import urlopen
from urllib.parse import urlencode,urljoin,quote,quote_plus,unquote,unquote_plus

host = "http://api.nnzhp.cn/?stu_name=小黑"
print(quote_plus(host))  #url编码
print(quote(host))

# url = urljoin(host,'/api/login')
url = "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=2&tn=baiduhome_pg&wd=%E4%BD%A0%E5%A5%BD%E6%96%BD%E5%B7%A5%E9%98%9F&rsv_spt=1&oq=sdgsdsdg&rsv_pq=cbca1b490021d566&rsv_t=83a3dvr2MgkSUHYndjTkUaly8mj0O2gEaU6FiQmNXisrrzrJ0W3nU6KvAPnPtdK%2FyfiU&rqlang=cn&rsv_enter=1&rsv_dl=tb&rsv_sug3=23&rsv_sug1=10&rsv_sug7=101&rsv_sug2=0&rsv_btype=t&inputT=4151&rsv_sug4=4151"

print(unquote(url))
print(unquote_plus(url))

 


url = "http:xxxxx"
data = {"stu_name":"xiaohei","age":18}
print(urlencode(data))
#req = urlopen(url,urlencode(data).encode() ) #post请求
req = urlopen(url+'?'+urlencode(data))
print(req.read().decode())

 

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

url = "http://xxxxx"
data = {"usernamz":"xxx","passwd":"xxx"}
data2 = {"version":1}
req = requests.post(url,params=data2,data=data)
print(req.url)  #查看请求发出去的url
#http://api.nnzhp.cn/api/user/login?version=1
#params参数是在url里面的
#data参数是在body里面的

url = "http://xxx"
data = {
       "grade": "nnn",
       "phone": "22345678992",
     "name":"哈哈哈哈"
 }
req = requests.post(url,json=data)

url = "https://qun.qq.com/cgi-bin/qun_mgr/search_group_members"
data = {
     "gc": 180208520,
     "st": "21",
     "end": "41",
     "sort": 0,
     "bkn": "1391997515"
 }
cookie = {'RK': 'XYYhgDpPfy', 'ptcz': 'a5b23b4f93bc2f304119957286b77b21be95814358d482ef0a2bf216733e57ba', 'pgv_pvid': '5931324545', 'uin': 'o0511402865', 'skey': '@HHempil2m', 'p_uin': 'o0511402865', 'pt4_token': 'tkeLBl-znMjpBJxIv1Chj*kLx7p0dsh4BkZfF39cpDk_', 'p_skey': 'OS7gS9Zw8*r*aIpqd5-S5BUo65EJMNWRkLLMO1Vpu5o_', 'traceid': '5b35fe5632'}

req = requests.post(url,data,cookies=cookie)

url = "https://qun.qq.com/cgi-bin/qun_mgr/search_group_members"
data = {
     "gc": 180208520,
     "st": "21",
     "end": "41",
     "sort": 0,
     "bkn": "1391997515"
 }
 header = {
     "cookie":"RK=XYYhgDpPfy; ptcz=a5b23b4f93bc2f304119957286b77b21be95814358d482ef0a2bf216733e57ba; pgv_pvid=5931324545; uin=o0511402865; skey=@HHempil2m; p_uin=o0511402865; pt4_token=tkeLBl-znMjpBJxIv1Chj*kLx7p0dsh4BkZfF39cpDk_; p_skey=OS7gS9Zw8*r*aIpqd5-S5BUo65EJMNWRkLLMO1Vpu5o_; traceid=5b35fe5632",
     "user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"
 }
 req = requests.post(url,data,headers=header)


 url = "http://api.nnzhp.cn/api/file/file_upload"
 data = {"file":open("a.xls",'rb')}
 req = requests.post(url,files=data)
 print(req.json())  #返回字典,如果接口返回的不是json,那会报错


url = "http://aliimg.changba.com/cache/photo/941190975_200_200.jpg"

req = requests.get(url,verify=False)

with open("a.jpg",'wb') as fw:
    fw.write(req.content)

 print(req.json())
 print(req.text) #返回的是字符串
 print(req.content) #返回的是bytes的
 print(req.status_code)  #返回的状态码
 print(req.cookies) #返回的cookie
 print(req.headers) #返回的header

这篇关于Python_网络请求的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!