Freebase查询使用python获取别名

时间:2014-12-05 06:56:31

标签: python-2.7 freebase

如何使用python从freebase获取名称中的别名列表?

例如,此查询

[{
  "name": "citigroup",
  "/common/topic/alias": []
}]

给出

{
  "result": [
    {
      "/common/topic/alias": [
        "Citigroup Inc.",
        "TRAVELERS GROUP INC",
        "CITIGROUP INC",
        "TRAVELERS INC",
        "Citi"
      ],
      "name": "Citigroup"
    },
    {
      "/common/topic/alias": [],
      "name": "Citigroup"
    }
  ]
}

我想要一个清单

["Citigroup Inc.",
"TRAVELERS GROUP INC",
"CITIGROUP INC",
"TRAVELERS INC",
"Citi"]

我试过

service_url = 'https://www.googleapis.com/freebase/v1/search'
query = {   "name": "citigroup", "/common/topic/alias": []}

params = [{ 'query': json.dumps(query), 'key': key, 'limit':5}]

url = service_url + '?' + urllib.urlencode(params)
response = json.loads(urllib.urlopen(url).read())

但是回复并没有产生任何结果......任何帮助都将不胜感激!感谢

1 个答案:

答案 0 :(得分:0)

试试这段代码

import urllib, json

service_url = 'https://www.googleapis.com/freebase/v1/mqlread'
query = [{ "name": "citigroup", "/common/topic/alias": []}]

params = { 'query': json.dumps(query), 'key': key, 'limit':5}

url = service_url + '?' + urllib.urlencode(params)
response = json.loads(urllib.urlopen(url).read())
print response

基本上应该在列表表单中询问查询,因为此查询有多个结果而不是字典表单。