我是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将类似于/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如果编码,就会得到相同的错误,因为所有括号都会被转换。专利视图的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"}}]}]}对于动态编程,我必须想出所有的库名。如果有更好的解决方案,请帮助。
诚挚的问候。
发布于 2017-09-06 03:21:48
api接受并返回json数据,因此您应该使用json.dumps对post数据进行编码。然后,如果您想要一个字典,或者只是写到文件中,那么在响应上使用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)正如克里斯蒂安建议的那样,您可以简单地使用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)发布于 2022-08-17 15:45:03
作为PatentsView的替代方案,请看一看patent_client!它是一个python模块,它使用Django风格的API搜索活跃的USPTO和EPO数据库。这包括支持PatentsView API的专利检查数据集。然后,任何查询的结果都可以通过简单的DataFrames调用转换成熊猫、.to_pandas()或Series。
from patent_client import USApplication
result = USApplication.objects.filter(first_named_inventor="<Name>")
# Returns an iterator of application objects matching the value.
# You can also go directly to a Pandas dataframe with:
result.to_pandas()一个很好的起点是用户指南简介
(完全披露-我是patent_client的作者和维护者)
https://stackoverflow.com/questions/46059088
复制相似问题