2016-11-18 100 views
0

我試圖讓手在使用文檔: 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>

+1

當我運行,而不是'curl'命令,我得到的完全相同的輸出。所以,請嘗試修復您的請求。 – Fejs

+0

[Post JSON using Python Requests]可能的重複(http://stackoverflow.com/questions/9733638/post-json-using-python-requests) –

+0

也許他們是服務器的問題。你的文檔中的 – furas

回答

2

嘗試轉換器,它可以轉換成curlrequests

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

看來它使用的URL與/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=