【问题标题】:translate curl query to requests将 curl 查询转换为请求
【发布时间】:2017-04-02 07:04:04
【问题描述】:

我试图在以下位置使用文档: 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>

【问题讨论】:

  • 当我运行 curl 命令时,我得到完全相同的输出。因此,请尝试解决您的请求。
  • 可能是服务器的问题。
  • 在您的文档中是示例 - 链接到显示查询结果的页面(在 Case 2 之前)它使用 API 获取数据,因此您可以在 Chrome/Frirefox 中使用 DevTools 来查看请求.他们甚至可以创建curl 代码。

标签: python curl


【解决方案1】:

尝试可以将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=

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-03
    • 2018-04-16
    • 2021-02-06
    • 2017-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-17
    相关资源
    最近更新 更多