福州市建网站公司,全网营销的四大优势,互联网公司做什么的,百度网盘电话人工服务项目背景#xff1a;通过curl 命令 从服务器获取数组#xff0c;tids#xff0c;然后导入到Mysql 表中。自己想的方案#xff1a;shell命令不熟悉#xff0c;另外python中的数据库等接口都已封装#xff0c;所以想使用Python来做#xff0c;利用Python调用shell命令。cu…项目背景通过curl 命令 从服务器获取数组tids然后导入到Mysql 表中。自己想的方案shell命令不熟悉另外python中的数据库等接口都已封装所以想使用Python来做利用Python调用shell命令。curl --header MLS: uid:xxxxx;ip:xxx.xx.xx v.mls.com/gl/get_gl -d pid409offset0limit480 #此为伪命令#返回值{error_code:0,data:{tids:[1746922055,1746931247,1708272843],usable_num:12082,show_num:12464}} # error_code0 表示正常Python中处理方案1 #os.system 最早了解到的执行系统命令的Python语句。cmd curl --header MLS: uid:xxxxx;ip:xxx.xx.xx v.mls.com/gl/get_gl -d pid409offset0limit480 #此为伪命令result json.loads( os.system(cmd) )result[data][tids] # 异常 TypeError: expected string or buffer。可能是os.system()命令返回的类型不符查了下Python文档On Unix, the return value is the exit status of the process encoded in the format specified for wait()方案2# commands.getoutput , 只返回执行结果忽略返回值。cmd curl --header MLS: uid:xxxxx;ip:xxx.xx.xx v.mls.com/gl/get_gl -d pid409offset0limit480 #此为伪命令result json.loads( commands.getoutput(cmd) )result[data][tids] #异常 ValueError: No JSON object could be decoded。打印出结果看一下还是有一堆乱起八糟的东西% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0{error_code:0,data:{tids:[1746127431,1762265107,1763545785,168808712100 5378 0 5346 100 32 18924 113 --:--:-- --:--:-- --:--:-- 1909283,1626151317,1717193707,1642206015,1701082599,1423118513,1474847025,1578211634,1743682339,1573758157,1671828749,1107839122,1671283222,1549615611,1626139235,1375268687,1664368093,1177596631],usable_num:3914,show_num:3914}}#加一个错误处理cmd curl --header MLS: uid:xxxxx;ip:xxx.xx.xx v.mls.com/gl/get_gl -d pid409offset0limit480 2/dev/null #此为伪命令result json.loads( commands.getoutput(cmd) )result[data][ties]##### 2/dev/null是如果你的命令出错的话错误报告直接就删除了不会显示在屏幕上 。搞定