首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将请求发送给extraction关系提取返回错误

将请求发送给extraction关系提取返回错误
EN

Stack Overflow用户
提问于 2015-03-09 10:37:50
回答 2查看 1.1K关注 0票数 0

在Bluemix中,我试图从Python调用extraction关系提取API。首先,我在Bluemix和bing上创建了一个应用程序,它是关系提取器api。然后,从API上的下拉菜单中,我从实例化凭据中获得用户名和密码。在下面的coe中,我已经用bluemux用户名和bluemix密码代替了它。我为此编写的Python代码如下:

代码语言:javascript
复制
import requests
import json

url="https://gateway.watsonplatform.net/relationship-extraction-beta/api/v1/sire/0"
username="bluemix_username"
password="bluemix_passowrd"
with open ("data.txt", "r") as myfile:
    text=myfile.read().replace('\n', '')

raw_data = {
    'contentItems' : [{
        'contenttype' : 'text/plain',
        'content': text
    }]
}

input_data = json.dumps(raw_data)


response = requests.post(url, auth=(username, password), headers =   {'content-type': 'application/json'}, data=input_data)
try:
    response.raise_for_status()
except requests.exceptions.HTTPError as e:
    print("And you get an HTTPError: %s"% e.message)

但是,当我运行这个程序时,我会得到以下错误:

代码语言:javascript
复制
And you get an HTTPError: 400 Client Error: Bad Request

*注:我在人格洞察力API中使用了同样的方法,而且效果很好。

有什么想法吗?

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-03-11 00:36:39

下面是您的代码的更新副本,应该可以工作:

代码语言:javascript
复制
import requests
import json

url="https://gateway.watsonplatform.net/relationship-extraction-beta/api/v1/sire/0"
username="bluemix_username"
password="bluemix_passowrd"
with open ("data.txt", "r") as myfile:
    text=myfile.read().replace('\n', '')

input_data = {
    'sid' : 'ie-en-news',
    'txt' : text
}

response = requests.post(url, auth=(username, password), data=input_data)
try:
    response.raise_for_status()
    print response.text
except requests.exceptions.HTTPError as e:
    print("And you get an HTTPError: %s"% e.message)

基本上,我更改了您发布的有效负载,以添加一些缺失的值。

希望这能有所帮助!

票数 1
EN

Stack Overflow用户

发布于 2015-05-09 04:49:51

如果您不想在终端中使用data.txt并使用标准输入,可以这样做:

代码语言:javascript
复制
## -*- coding: utf-8 -*-

import os
import requests
import fileinput

class RelationshipExtractionService:
    url = None

    def __init__(self):
        self.url = "https://gateway.watsonplatform.net/relationship-extraction-beta/api/v1/sire/0"
        self.user = "<username>"
        self.password = "<password>"

    def extract(self, text):
        data = {
            'txt': text,
            'sid': 'ie-en-news',  # English News, for Spanish use: ie-es-news
            'rt': 'xml',
        }

        r = requests.post(self.url,
                          auth=(self.user, self.password),
                          headers = {
                              'content-type': 'application/x-www-form-urlencoded'},
                          data=data
                          )
        print("Request sent. Status code: %d, content-type: %s" %
              (r.status_code, r.headers['content-type']))
        if r.status_code != 200:
            print("Result %s" % r.text)
            raise Exception("Error calling the service.")
        return r.text

if __name__ == '__main__':
    service = RelationshipExtractionService()
    for line in fileinput.input():
        print service.extract(line)

使用

简单的文本分析:

echo "New York is awesome" | python main.py

您还可以通过管道传输文件:

cat article.txt | python main.py

从.txt到.xml:

cat article.txt | python main.py > article.xml

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28939928

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档