【Ruby爬虫06】企业基本信息查询

查询结果

查询接口

ruby 复制代码
# frozen_string_literal: true  
  
require 'http'  
require 'json'  
  
def query(company_name)  
  # 获取基本信息  
  headers = {  
    'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36',  
    'version' => 'TYC-XCX-WX',  
    'Host' => 'api9.tianyancha.com',  
    'Authorization' => '0###oo34J0VKzLlpdvf8kgFkMlfU_IPY###1642087379312###22494f3155c2e5a4be76e503837fa439',  
    'x-auth-token' => 'eyJkaXN0aW5jdF9pZCI6IjE3ZDFjNWVhMzZjNGY2LTA5ZjU2NWUwNWViNTZjLTFjMzA2ODUxLTIwNzM2MDAtMTdkMWM1ZWEzNmRiMzYiLCJsaWIiOnsiJGxpYiI6ImpzIiwiJGxpYl9tZXRob2QiOiJjb2RlIiwiJGxpYl92ZXJzaW9uIjoiMS4xNS4yNCJ9LCJwcm9wZXJ0aWVzIjp7IiR0aW1lem9uZV9vZmZzZXQiOi00ODAsIiRzY3JlZW5faGVpZ2h0IjoxMDgwLCIkc2NyZWVuX3dpZHRoIjoxOTIwLCIkbGliIjoianMiLCIkbGliX3ZlcnNpb24iOiIxLjE1LjI0IiwiJGxhdGVzdF90cmFmZmljX3NvdXJjZV90eXBlIjoi6Ieq54S25pCc57Si5rWB6YePIiwiJGxhdGVzdF9zZWFyY2hfa2V5d29yZCI6IuacquWPluWIsOWAvCIsIiRsYXRlc3RfcmVmZXJyZXIiOiJodHRwczovL3d3dy5nb29nbGUuY29tLyIsImN1cnJlbnRfdXJsIjoiaHR0cHM6Ly93d3cudGlhbnlhbmNoYS5jb20vc2VhcmNoP2tleT0lRTYlOUQlQUQlRTUlQjclOUUlRTYlOTklQUUlRTUlODUlQjQlRTQlQkMlODElRTQlQjglOUElRTclQUUlQTElRTclOTAlODYlRTUlOTAlODglRTQlQkMlOTklRTQlQkMlODElRTQlQjglOUEiLCJyZWZlcnJlciI6Imh0dHBzOi8vd3d3LnRpYW55YW5jaGEuY29tL3NlYXJjaD9rZXk9JUU2JTlEJUFEJUU1JUI3JTlFJUU2JTk5JUFFJUU1JTg1JUI0JUU0JUJDJTgxJUU0JUI4JTlBJUU3JUFFJUExJUU3JTkwJTg2JUU1JTkwJTg4JUU0JUJDJTk5JUU0JUJDJTgxJUU0JUI4JTlBIiwidHljaWQiOiI0MmMxZTY1MDQ0ZjYxMWVjYmIxZDY3ZmJiYzEwN2U3NSIsIm5hbWUiOiLmna3lt57mma7lhbTkvIHkuJrnrqHnkIblkIjkvJnkvIHkuJoiLCJtb2R1bGUiOiLkvJjotKjlrp7lkI3orqTor4EiLCIkaXNfZmlyc3RfZGF5IjpmYWxzZX0sImFub255bW91c19pZCI6IjE3ZDFjNWVhMzZjNGY2LTA5ZjU2NWUwNWViNTZjLTFjMzA2ODUxLTIwNzM2MDAtMTdkMWM1ZWEzNmRiMzYiLCJ0eXBlIjoidHJhY2siLCJldmVudCI6InNlYXJjaF9yZXN1bHRfZXhwdXJlIiwiX3RyYWNrX2lkIjo3MjUyNDM3Mjd9'  
  }  
  url = "https://api9.tianyancha.com/services/v3/search/sNorV3/#{company_name}"  
  response = HTTP.get(url, :headers=>headers)  
  response_json = JSON.parse(response.body)  
  
  data = {}  
  if response_json['state'] == 'ok'  
    data = response_json['data']  
  end  
  # puts data  
  
  # 基本信息提取  
  if data.size > 0  
    # 默认取查询结果第一个  
    company = data['companyList'][0]  
    brand_and_agency = {}  
    data['brandAndAgencyList'].each do |item|  
      if item['graphId'] == company['id']  
        brand_and_agency = item  
        break  
      end    
    end  
    # 获取企业详细信息  
    url = "https://api9.tianyancha.com/services/v3/t/common/baseinfoV5/#{brand_and_agency['graphId']}"  
    detail = JSON.parse(HTTP.get(url, :headers=>headers).body)['data']  
    puts detail  
  else  
    puts "未查询到企业信息"  
  end  
end  
  
# 将hash转换为a=1&b=2&c=3的查询字符串  
def hash_to_query_string(hash)  
  hash.map{|k,v| "#{k}=#{v}"}.join("&")  
end  
  
  
if __FILE__ == $0  
  query('中国铁建')  
end
相关推荐
雨颜纸伞(hzs)10 分钟前
C语言介绍
c语言·开发语言·软件工程
J总裁的小芒果12 分钟前
THREE.js 入门(六) 纹理、uv坐标
开发语言·javascript·uv
坊钰41 分钟前
【Java 数据结构】移除链表元素
java·开发语言·数据结构·学习·链表
chenziang11 小时前
leetcode hot100 LRU缓存
java·开发语言
时雨h1 小时前
RuoYi-ue前端分离版部署流程
java·开发语言·前端
云计算DevOps-韩老师1 小时前
【网络云计算】2024第52周-每日【2024/12/25】小测-理论&实操-自己构造场景,写5个系统管理的脚本-解析
开发语言·网络·云计算·bash·perl
暮色尽染1 小时前
Python 正则表达式
开发语言·python
IT猿手1 小时前
最新高性能多目标优化算法:多目标麋鹿优化算法(MOEHO)求解GLSMOP1-GLSMOP9及工程应用---盘式制动器设计,提供完整MATLAB代码
开发语言·算法·机器学习·matlab·强化学习
小爬虫程序猿1 小时前
利用Java爬虫获取速卖通(AliExpress)商品详情的详细指南
java·开发语言·爬虫
xlsw_2 小时前
java全栈day21--Web后端实战之利用Mybaits查询数据
java·开发语言