1. 程式人生 > 實用技巧 >正則取值及斷言實戰示例

正則取值及斷言實戰示例

demo_01.py

import requests
import re
session = requests.session()
get_param_dict={
"grant_type":"client_credential",
"appid":"wx55614004f367f8ca",
"secret":"65515b46dd758dfdb09420bb7db2c67f"
}
response = session.get( url='https://api.weixin.qq.com/cgi-bin/token',
params=get_param_dict)
response.encoding = response.apparent_encoding
body = response.text
print( body )
value = re.findall('"access_token":"(.+?)"',body)[0]
print( value )

post方法:

介面斷言設計:

斷言設計思路:

檢查多個key是否存在:

兩個都是true,說明2個都存在。

這裡至始至終要保證出一個結果:萬一結果覆蓋或者出現其他情況就不對。處理:

這種也不對:在迴圈第一次的時候不滿足是false,迴圈第2次的時候是滿足的是true.因為檢查的是多個key,所以把結果覆蓋掉了,就不對。這種方法無法判斷yes和no。

調整:當它等於access_toke不包含n的,for迴圈檢查不到的時候就在列表yes_no =[]裡面增加一個false,當它滿足expires_in的時候就增加一個true。

不管我們這裡檢查了多少個key,如果檢查的過程中出現了一次,一種,或者一個false,那麼這次檢查就是失敗的。