使用请求模块访问api

时间:2018-01-08 09:08:48

标签: python python-3.x

curl命令按预期工作。但类似的python代码却没有。

# curl -X POST -H "Content-Type: application/json" -d '{"img_url":"http://tleyden-misc.s3.amazonaws.com/blog_images/ocr_test.pnge","engine":"tesseract"}' http://35.154.148.131:9292/ocr

预期产出:

You can create local variables for the pipelines within the template by
prefixing the variable name with a "$" sign. Variable names have to be
composed of alphanumeric characters and the underscore. In the example
below I have used a few variations that work for variable names.

我试过这个python代码:

import requests
url = 'http://35.154.148.131:9292'
data = {"img_url":"http://tleyden-misc.s3.amazonaws.com/blog_images/ocr_test.png","engine":"tesseract"}  
r = requests.post( url, json={'json_payload': data})

这会返回以下错误:

b'<h1>OpenOCR is running!<h1> Need <a href="http://www.openocr.net">docs</a>?'

如何使用数据参数发布字典?

2 个答案:

答案 0 :(得分:2)

这就是我设法做到这一点的方法:

import requests
url = 'http://35.154.148.131:9292/ocr'
data = {"img_url":"http://tleyden-misc.s3.amazonaws.com/blog_images/ocr_test.png","engine":"tesseract"}  

r = requests.post( url, json=data )

答案 1 :(得分:1)

import requests 
url = 'http://35.154.148.131:9292/ocr'
data = {"img_url":"http://tleyden-misc.s3.amazonaws.com/blog_images/ocr_test.png","engine":"tesseract"}
r = requests.get(url, json=data) 
r.text

结果

u'You can create local variables for the pipelines within the template by\nprefixing the variable name with a "$" sign. Variable names have to be\ncomposed of alphanumeric characters and the underscore. In the example\nbelow I have used a few variations that work for variable names.\n\n'