1,那只函数的补充,内容
1 #zip 2 li=['a','b','c','e','f'] 3 l2=[1,2,3] 4 l3=['A','S','D'] 5 l4=['哈','哈','发'] 6 #zip就是把两个list,合并到一起,如果想同时循环两个list的时候, 7 for a,b,c,d in zip(li,l2,l3,l4): 8 print(a,b,c,d) 9 10 #map 他是帮我们循环调用函数的 11 def my(num): 12 return str(num) 13 14 lis=[1,2,3,4,5,6,7,8,9] 15 res=list(map(my,lis)) 16 print(res) 17 18 # filter 也是帮你循环调用函数的,过滤 19 def even(num): 20 if num%2==0: 21 return True 22 return False 23 lis=[1,2,3,4,5,6,7,8,9] 24 res= filter(even,lis) 25 print(list(res))#filter只保留,返回数据为真的数据 26 res2 = map(even,lis) 27 print(list(res2))#map是帮你循环调用函数,这个函数返回就保存什么
2.异常处理:
1 first = input('请输入除数:') 2 second = input('请输入被除数:') 3 try: 4 first=int(first) 5 second=int(second) 6 res = first/second 7 li = [1,2,3,4] 8 print(li[9]) 9 # except ValueError as e:#e代表错误信息,如果上面的两行代码出现了ValueError错误就走下面 10 # print(e) 11 # print('请输入整数') 12 # except ZeroDivisionError as e: 13 # print('除数不能为零') 14 except Exception as e:#代码异常的时候走这里 15 print(e) 16 print('出错了') 17 else:#没出现异常走这里,不是必须写的,可以直接鞋上面直接输出即可 18 print('没有异常') 19 print(res) 20 21 finally:#不管出错还是不出错都会执行,不是必须写的 22 print('我是finally')
3.好用的网络编程
首先需要通过命令安装pip install requests
1 import requests 2 3 #1.发送get请求 4 url = 'http://api.nnzhp.cn/api/user/stu_info' 5 data = {'stu_name':'小黑'}#请求参数 6 req=requests.get(url,params=data)#发送get请求 7 print(req.json())#字典类型 8 print(req.text)#返回的是json串 9 10 #2.发送post请求 11 url = 'http://api.nnzhp.cn/api/user/login' 12 data = {'username':'yjy12345','passwd':'aA123456'}#请求参数 13 req=requests.post(url,data)#发送post请求 14 print(req.json()) 15 16 #3.入参是json类型 17 import random 18 phone = random.randint(10000000000,99999999999) 19 url = 'http://api.nnzhp.cn/api/user/add_stu' 20 data = { 21 "name":"小yu", 22 "grade":"天蝎座", 23 "phone":phone, 24 "sex":"男", 25 "age":28, 26 "addr":"河南省济源市北海大道32号" 27 } 28 req=requests.post(url,json=data) 29 print(req.json()) 30 31 #4.添加cookie的 32 url = 'http://api.nnzhp.cn/api/user/gold_add' 33 data = { 34 "stu_id":468, 35 "gold":100 36 } 37 cookie = {'niuhanyang':'337ca4cc825302b3a8791ac7f9dc4bc6'} 38 req=requests.post(url,data,cookies=cookie) 39 print(req.json()) 40 #5.添加header 41 42 url = 'http://api.nnzhp.cn/api/user/all_stu' 43 header = {'Referer':'http://api.nnzhp.cn/'} 44 req=requests.get(url,headers=header) 45 print(req.json()) 46 47 #6.上传文件 48 url = 'http://api.nnzhp.cn/api/file/file_upload' 49 # data = {'file':open('baidu.html',encoding='utf-8')}#上传文件 50 data = {'file':open(r'D:用户目录我的图片240425-12101219332061.jpg','rb')}#上传图片用rb模式 51 req=requests.post(url,files=data) 52 print(req.json()) 53 54 #7.下载文件,图片,歌曲 55 url='http://www.nnzhp.cn/wp-content/uploads/2018/01/soup.jpg' 56 req = requests.get(url) 57 print(req.content)#返回一个二进制的 58 fw=open('s.jpg','wb') 59 fw.write(req.content)
稍微不用使用,但也能实现功能的网络编程
1 #这个模块不好用 2 from urllib import request,parse 3 url = 'http://www.nnzhp.cn' 4 req=request.urlopen(url)#打开一个url,发get请求 5 content=req.read().decode()#获取返回结果 6 fw=open('baidu.html','w',encoding='utf-8') 7 fw.write(content) 8 # 打开一个接口,这个是get请求 9 import json 10 url = 'http://api.nnzhp.cn/api/user/stu_info?stu_name=xiaohei' 11 req = request.urlopen(url) 12 content = req.read().decode() 13 res_dic=json.loads(content)#把返回的结果转为字典 14 if res_dic.get('error_code')==0: 15 print('测试通过') 16 else: 17 print('测试失败',res_dic) 18 19 20 url = 'http://api.nnzhp.cn/api/user/login' 21 data = { 22 'username':'admin', 23 'passwd':'aA123456' 24 }#请求数据 25 data=parse.urlencode(data)#urlencode自动帮你拼好参数 26 req=request.urlopen(url,data.encode())#发送post请求 27 print(req.read().decode())