Skip to main content
 首页 » 编程设计

python之Patentsview API Python 3.4

2025年02月15日8zfyouxi

我是 Python 初学者,目前正在使用 Python 开发一个小项目。 我想为 patentsview.org 构建用于专利研究的动态脚本。

这是我的代码:

import urllib.parse 
import urllib.request 
 
#http://www.patentsview.org/api/patents/query?q={"_and": 
[{"inventor_last_name":author},{"_text_any":{"patent_title":[title]}}]}&o= 
{"matched_subentities_only": "true"} 
author = "Jobs" 
andreq = "_and" 
invln = "inventor_last_name" 
text = "_text_any" 
patent = "patent_title" 
match = "matched_subentities_only" 
true = "true" 
title = "computer" 
urlbasic = "http://www.patentsview.org/api/patents/query" 
patentall = {patent:title} 
textall = {text:patentall} 
invall = {invln:author} 
andall = invall.copy() 
andall.update(textall) 
valuesq = {andreq:andall} 
valuesqand = {andreq:andall} 
valuesq = {andreq:valuesqand} 
valueso = {match:true} 
 
######### 
url = "http://www.patentsview.org/api/patents/query" 
values = {"q":valuesq, 
          "o":valueso} 
print(values) 
 
 
data = urllib.parse.urlencode(values) 
print(data) 
############ 
data = data.encode("UTF-8") 
print(data) 
req = urllib.request.Request(url,data) 
resp = urllib.request.urlopen(req) 
respData = resp.read() 
saveFile = open("patents.txt", "w") 
saveFile.write(str(respData)) 
saveFile.close() 

我认为我的动态 URL 是正确的开始 - 但编码似乎给我一个 HTTP 错误 400:错误请求。 如果我不编码,url 将类似于 www.somethingsomething.org/o:{....}这显然会产生错误。 这是错误:

Traceback (most recent call last): 
  File "C:/Users/Max/PycharmProjects/KlayerValter/testen.py", line 38, in  
<module> 
resp = urllib.request.urlopen(req) 
File "C:\Python34\lib\urllib\request.py", line 161, in urlopen 
return opener.open(url, data, timeout) 
  File "C:\Python34\lib\urllib\request.py", line 469, in open 
response = meth(req, response) 
  File "C:\Python34\lib\urllib\request.py", line 579, in http_response 
'http', request, response, code, msg, hdrs) 
File "C:\Python34\lib\urllib\request.py", line 507, in error 
return self._call_chain(*args) 
  File "C:\Python34\lib\urllib\request.py", line 441, in _call_chain 
result = func(*args) 
File "C:\Python34\lib\urllib\request.py", line 587, in http_error_default 
raise HTTPError(req.full_url, code, msg, hdrs, fp) 
urllib.error.HTTPError: HTTP Error 400: Bad Request 
 
Process finished with exit code 1 

如果我编码,我会得到同样的错误,因为所有的括号都被转换了。 patentsview的API工作原理如下:

http://www.patentsview.org/api/patents/query?q={"_or":[{"_and": 
[{"inventor_last_name":"Whitney"},{"_text_phrase":{"patent_title":"cotton  
gin"}}]},{"_and":[{"inventor_last_name":"Hopper"},{"_text_all": 
{"patent_title":"COBOL"}}]}]} 

对于动态规划,我必须想出所有的库名称。如果还有更好的解决方案,请帮忙。

最好的问候。

请您参考如下方法:

该 api 接受并返回 json 数据,因此您应该使用 json.dumps对您的帖子数据进行编码。然后使用 json.loads如果你想要一本字典,或者只是写入文件,就在回复上。

from urllib.request import Request, urlopen 
import json 
 
url = "http://www.patentsview.org/api/patents/query" 
author = "Jobs" 
title = "computer" 
data = { 
    'q':{ 
        "_and":[ 
            {"inventor_last_name":author}, 
            {"_text_any":{"patent_title":title}} 
        ] 
    },  
    'o':{"matched_subentities_only": "true"} 
} 
resp = urlopen(Request(url, json.dumps(data).encode())) 
data = resp.read() 
#data = json.loads(data) 

正如 Christian 所建议的,您可以简单地使用 requests ,它比 urllib 好得多。

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

至于您代码中的所有这些变量,它们组成了一个字典,如下所示:

values = {"q":{andreq:{andreq:{invln:author, text:{patent:title}}}}, "o":{match:true}} 

我不明白您为什么要费尽心思来构建字典,但我可能是错的。但是,您可以将代码包装在一个以 author 和 title 作为参数的函数中。


使用 requests,您不必对数据使用 json.dumps,只需使用 json 参数即可。如果您想将响应内容保存到文件中,您应该使用 content 或 text 属性。

import requests 
 
title = "computer"  
author = "Jobs"  
url = "http://www.patentsview.org/api/patents/query"  
data = {  
    "q":{ "_and":[ {"inventor_last_name":author}, {"_text_any":{"patent_title":title}}] },  
    "o":{"matched_subentities_only":"true"}  
}  
resp = requests.post(url, json=data)  
with open("patents.txt", "w") as f: 
    f.write(resp.text)