将curl查询转换为请求

时间:2016-11-18 12:15:51

标签: python curl

我试图使用以下文档: https://pairbulkdata.uspto.gov/#/api-documentation

然而,当我尝试这些查询时,我收到了错误消息。 我正在尝试将curl查询转换为python请求。

curl -i -X POST -H "Content-Type:application/json" -d '{"searchText":"medicine AND diabetes","qf": "patentTitle"}' http://pairbulkdata.uspto.gov/queries

这是我正在尝试的python代码:

import requests

data = {"searchText":"medicine AND diabetes","qf": "patentTitle"}

url = "http://pairbulkdata.uspto.gov/queries"

header = {"Content-Type":"application/json"}

r = requests.post(url, data = data, headers=header)

但我得到错误。



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">\n<HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">\n<TITLE>ERROR: The request could not be satisfied</TITLE>\n</HEAD><BODY>\n<H1>ERROR</H1>\n<H2>The request could not be satisfied.</H2>\n<HR noshade size="1px">\nBad request.\n<BR clear="all">\n<HR noshade size="1px">\n<PRE>\nGenerated by cloudfront (CloudFront)\nRequest ID: OIhwX7a3zVJq04M_qf0sjWhuke3fHb1-6wFJsN7UX_Rp2w_gzebTGA==\n</PRE>\n<ADDRESS>\n</ADDRESS>\n</BODY></HTML>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

尝试将curl转换为requests

的转换器

http://curl.trillworks.com/

https://shibukawa.github.io/curl_as_dsl/index.html

修改

我检查了文档,并且有指向使用API​​获取数据的页面的链接

https://pairbulkdata.uspto.gov/#/search?q=medicine%20AND%20diabetes&sort=applId%20asc

它似乎使用了/api/queries的网址,但文档显示/queries

-

此代码为我提供了一些数据作为JSON - 所以它可能正常工作

curl 'https://pairbulkdata.uspto.gov/api/queries' -X POST -H 'Content-Type: application/json' -d '{"searchText":"medicine AND diabetes","qf": "patentTitle"}'

-

这也给出了一些结果。

import requests

url = "https://pairbulkdata.uspto.gov/api/queries"

headers = {"Content-Type":"application/json"}

data = {"searchText":"medicine AND diabetes","qf": "patentTitle"}

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

print(r.text)

我用

  • https://代替http://
  • /api/queries代替/queries
  • json=代替data=