import urllib.parse
s= ' besttest automation test '
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 (' Enter how much: ')
months = input (' Also several months: ')
Try:
res = calc (int (money), int (months))
except Zerodivisionerror as e: #try里面的代码如果出错了, go except inside the code
traceback.print_exc () #只是输出报错的详细信息而已
print (' The number of months to repay cannot be less than 1 ', e)
except ValueError as E:
print (' Input must be an integer,%s '%e)
except Exception as e: #捕获所有的异常
Print (' Unknown error! %s '%e)
Else: #没有出错的情况下走else
print (' should also%s '%res per month)
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 (' database not connected,%s '%e)
Else:
cur = conn.cursor ()
Try:
cur.execute (SQL)
except Exception as E:
print (' SQL statement has errors! %s SQL is '%s '% (e,sql))
Else:
res = Cur.fetchall ()
return res
finally: #不管有没有捕捉到异常, will go here.
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 (' When do I get here? ')
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 (' Interface not data ') #主动抛出异常
Req ()
Day8-python Learning Notes (17) URL encoding, exception handling