上篇學習sys.argv的使用代碼中發現了錯誤,獲取到的Json數據為單引號和NONE,為錯誤的json格式。
經檢查,發現直接使用r.text就可以得到正確的json數組,使用r.json()反而使json數組中的雙引號變為單引號,null變為NONE。
原因為.json()會改變正確格式的json數組,需要dump()處理
修改后代碼如下
import requests import sys url = 'http://www..com//Order/' def query(companyid, he_type, he_no): d = {'companyid': companyid, 'type': he_type, 'no': he_no} r = requests.post(url, data=d) return r.text if __name__ == '__main__': company_id = sys.argv[1] the_type = sys.argv[2] the_no = sys.argv[3] print(query(company_id, the_type, the_no)) # print(query("", 1, ""))
