python中的FRED rest API

时间:2014-03-20 08:55:05

标签: java python python-2.7

我们正在使用FRED将纯数据转换为RDF / XML格式。我们在网上找到了JAVA代码,可以为我们提供PYTHON代码。 我找到的JAVA代码是:

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

public class TestGet
{
    public static void main(String[] args) 
    {
        try 
    {
            Client client = Client.create();
            WebResource webResource = client.resource("http://wit.istc.cnr.it/stlab-tools/fred/");
            ClientResponse response = webResource.queryParam("text", "He is a good boy").accept("application/rdf+xml").get(ClientResponse.class);
            if (response.getStatus() != 200) 
            {
                throw new RuntimeException("Failed : HTTP error code : "+response.getStatus());
            }
            String output = response.getEntity(String.class);
            System.out.println("Output from Server .... \n");
            System.out.println(output);
        } 
    catch (Exception e) 
        {
            e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

对于这样一个简单的案例,urllib2可能是最简单的选择。

import urllib2
import urllib # needed for url-encoding
par = {"text": "He is a good boy"}
headers={"Accept" : "application/rdf+xml"}
request = urllib2.Request(
    "http://wit.istc.cnr.it/stlab-tools/fred/?" + urllib.urlencode(par),
    headers=headers)
contents = urllib2.urlopen(request).read()
print contents

或许你想和Requests一起去,如果事情变得更复杂,这会更酷:

import requests
par = {"text": "He is a good boy"}
headers={"Accept" : "application/rdf+xml"}
r = requests.get("http://wit.istc.cnr.it/stlab-tools/fred/",
    data=par, headers=headers)
print r["text"]