day8-Python学习笔记(十七)url编码,异常处理

import urllib.parse
s='besttest 自动化测试'
print(urllib.parse.quote(s)) #url编码
print(urllib.parse.quote_plus(s)) #url编码,
src='https://www.baidu.com/s?wd=besttest%20%E8%87%AA%E5%8A%A8%E5%8C%96%E6%B5%8B%E8%AF%95&rsv_spt=1&rsv_iqid=0x90f615640005e9b2&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&rqlang=cn&tn=baiduhome_pg&rsv_enter=1&inputT=8129&rsv_t=0d22VbBsLUg6wkL0kNn1rPhfwhU%2F5UCRNiEBd5oqMzEh6ImgExR7fH65dNLwCf0ilzUT&rsv_sug3=36&rsv_sug1=9&rsv_sug7=100&oq=besttest&rsv_pq=f857c55a00063cfd&rsv_sug2=0&rsv_sug4=8623'

print(urllib.parse.unquote(src)) #url解码
print(urllib.parse.unquote_plus(src)) #url解码


import traceback
def calc(a,b):
res = a/b
return res

def main():
money = input('输入多少钱:')
months = input('还几个月:')
try:
res = calc(int(money),int(months))
except ZeroDivisionError as e: #try里面的代码如果出错了,走except里面的代码
traceback.print_exc()#只是输出报错的详细信息而已
print('还款的月数不能小于1',e)
except ValueError as e:
print('输入必须是整数,%s'%e)
except Exception as e: #捕获所有的异常
print('未知错误!%s'%e)
else:#没有出错的情况下走else
print('每个月应该还%s'%res)
print('hahahahahahah')

import pymysql
def main2(sql):
try:
conn=pymysql.connect(host='122.932.122.11',user='root',password='123456',db='test')
except Exception as e:
print('数据库连接不了,%s'%e)
else:
cur = conn.cursor()
try:
cur.execute(sql)
except Exception as e:
print('sql语句有错误!%s。sql是"%s'%(e,sql))
else:
res = cur.fetchall()
return res
finally: #不管有没有捕捉到异常,都会走这里。
cur.close()
conn.close()


# try:
# a = int(input('xx:'))
# b = int(input('sss:'))
# res = a/b
# except Exception as e:
# print(e)
# else:
# print(res)
# finally:
# print('什么时候到我这里呢')
import requests
def req():
r = requests.get('http://api.nnzhp.cn/api/user/all_stu',headers={'Referer':'http://api.nnzhp.cn/'})
if len(r.json()['stu_info'])<0:
pass
else:
raise Exception('接口没数据') #主动抛出异常

req()
 

转载于:https://www.cnblogs.com/flynn0825/p/8419581.html

你可能感兴趣的:(day8-Python学习笔记(十七)url编码,异常处理)