post跟get的区别

(1)通过post方式获取行业名称

 @staticmethod
    def hy_info_company(company_name):
        """
        根据公司名获取行业映射的辅助字段
        see wiki http://192.168.31.157:8200/doku.php?id=huangyu:网络图:中金全库网络图
        """
        url_hy_info_company_format = GET_HY_MAP_INFO + u'?companyName={}'
        url = url_hy_info_company_format.format(company_name)
        source = ['company', 'province', 'city', 'scale', 'industryType']
        data = {
            'feaType': 'custom',
            'companyNameList': [company_name],
            'source': source,
            'residentMapping': 'yes'}
        return ApiHelper.post_url_dict(url, data)

理解, ApiHelper.post_url_dict(url, data)中url其实就是一个http的网址,data相当于限定了根据网址取出的特定字段的数据,下面是取上述post获取回来数据的方式:

big_hyname = Stream(improve_hy_map_api.hy_info_company(company_name)) \
                        .take_path('result', []) \
                        .head({}) \
                        .take_path('industryType') \
                        .get()
 take_path('result', []):根据路径在dict中取值,反推数据结构为{'result':[...],}
 head({}):取第一个;反推数据结构为{'result':[{},{},...],}
 take_path('industryType'):根据路径在dict中取值,反推数据结构为{'result':[{'industryType':XX},{},...],},其中XX有可能是[XX]或者‘XX’的形式返回行业名称。

(2)以get的方式获取网页数据

    def get_cmp_basic_api(self, company_name):
        """
        公司的基本信息:
        网络图api
        see wiki: http://192.168.31.157:8200/doku.php?id=huangyu:%E5%B7%A5%E5%95%86:%E6%B4%9E%E6%82%89api
        基本信息(免费)    (提供上面接口的免费版)    /api/saic/dongxi/free   /api/saic/dongxi/free?companyName=北京龙盛源小额贷款有限公司
        :return:
        """
        url_network_company_format = GET_CMP_BASIC_INFO + u'{}'
        url = url_network_company_format.format(company_name)
        data = APiH.get_url_dict(url)
        return self.__data_verify(data)

直观区别:get方式不需要输入数据,限定取哪些指定字段。

(3) 关于get和post的封装类。
对get或post得到的数据进行json格式转换

import requests
from requests import Timeout

from common.exceptions.api_exception import ApiException
from common.exceptions.data_exception import DataException
from scpy.logger import get_logger
import json

logging = get_logger(__file__)


class ApiHelper(object):
    """
    api 辅助器
    """

    @staticmethod
    def get_url_dict(url):
        """
        get 请求返回字典表
        :type url str
        """
        try:
            logging.info('request:' + url)
            response = requests.get(url, timeout=120, headers={'Connection': 'keep-alive'})
            logging.info('response:' + url)
            return response.json() if response.ok else {}
        except Timeout as e:
            logging.error('error url: %s', url)
            logging.error('error message: %s', e.message)
            raise ApiException(message=u"api请求超时", code=ApiException.CODE_TIMEOUT, inner_exception=e)
        except Exception as e:
            logging.error('error url: %s', url)
            logging.error('error message: %s', e.message)
            raise DataException(inner_exception=e)

    @staticmethod
    def post_url_dict(url, data):
        """
        get 请求返回字典表
        :param data:
        :type url str
        """
        try:
            headers = {'Content-Type': 'application/json',
                       'Connection': 'keep-alive'}

            logging.info('request:' + url)
            response = requests.post(url, data=json.dumps(data), timeout=120, headers=headers)
            logging.info('response:' + url)
            return response.json() if response.ok else {}
        except Timeout as e:
            logging.error('error url: %s', url)
            logging.error('error message: %s', e.message)
            raise ApiException(message=u"api请求超时", code=ApiException.CODE_TIMEOUT, inner_exception=e)
        except Exception as e:
            logging.error('error url: %s', url)
            logging.error('error message: %s', e.message)
            raise ApiException(message=u"api请求未知错误", inner_exception=e)


if __name__ == '__main__':
    from api.api_utils.api_helper import ApiHelper as apiH
    from conf.config import *
    import json

    res = apiH.get_url_dict(GET_CMP_BASIC_INFO + "北京龙盛源小额贷款有限公司")
    print (json.dumps(res, ensure_ascii=False, indent=4))

3)自己定义的异常:
继承Exception基类,并且还用到了 super(ApiException, self).init ,学习…
默认是内部异常, init(self, message=None, code=INTERNAL_SERVER_ERROR, ….)

class ApiException(Exception):
    # api 超时
    CODE_TIMEOUT = 408

    # 500 内部异常
    INTERNAL_SERVER_ERROR = 500

    def __init__(self, message=None, code=INTERNAL_SERVER_ERROR, inner_exception=None, **kwargs):
        self.message = message
        self.code = code
        self.inner_exception = inner_exception
        super(ApiException, self).__init__(message, code, inner_exception, **kwargs)

猜你喜欢

转载自blog.csdn.net/sinat_26566137/article/details/80861720
今日推荐