获取行业字段方式,api的post形式

def get_company_industry_scale(company_name):
    """
    get company industry and scale from profile API
    """
    **session = requests.session()**
    info_url = GET_INDUSTRY_URL.format(company_name)
    **response = session.post(
        url=info_url,
        data=json.dumps(
            {
                'feaType': 'custom',
                'source': ['province', 'city', 'industryType', 'scale'],
                'companyNameList': [company_name]
            }
        )
    )**
    if response.status_code == 200:
        dataInfo = response.json().get('result', None)
        if dataInfo:
            industryType = dataInfo[0].get('industryType', None)
            scale = dataInfo[0].get('scale', None)
            return {'industryType': industryType, 'scale': scale}
        else:
            return {}
    else:
        return {}

方式2:

   @staticmethod
    def hy_info_company(company_name):
        """
        根据公司名获取行业映射的辅助字段
        see wiki http://192.168.31.157:8200/doku.php?id=huangyu:%E7%BD%91%E7%BB%9C%E5%9B%BE:%E4%B8%AD%E9%87%91%E5%85%A8%E5%BA%93%E7%BD%91%E7%BB%9C%E5%9B%BE
        """
        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)
     @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)
  取数据出来:
  improve_hy_map_api = ImproveHyMapApiSvc()
   big_hyname = Stream(improve_hy_map_api.hy_info_company(company_name)) \
                        .take_path('result', []) \
                        .head({}) \
                        .take_path('industryType') \
                        .last(self.evaluate_consts.NAN) \
                        .get()
api取数据方式1:
 session = requests.session()
 info_url = GET_INDUSTRY_URL.format(company_name)
 response = session.post(
        url=info_url,
        data=json.dumps(
            {
                'feaType': 'custom',
                'source': ['province', 'city', 'industryType', 'scale'],
                'companyNameList': [company_name]
            }
        )
    )
api取数据方式2:
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'}
headers = {'Content-Type': 'application/json',  'Connection': 'keep-alive'}
response = requests.post(url, data=json.dumps(data), timeout=120, headers=headers)

猜你喜欢

转载自blog.csdn.net/sinat_26566137/article/details/81076147