Patentsview API Python 3.4

Patentsview API Python 3.4,python,api,urllib,Python,Api,Urllib,我是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]}}]}&

我是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
对于动态编程,我必须列出所有库的名称。如果还有更好的解决方案,请帮助


致以最诚挚的问候。

api接受并返回json数据,因此您应该使用它对post数据进行编码。然后,如果您想要一个字典,或者只是写入文件,请在响应上使用

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所建议的,您可以简单地使用它,它比
urllib
好得多

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

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

values = {"q":{andreq:{andreq:{invln:author, text:{patent:title}}}}, "o":{match:true}}
我不明白你为什么要费尽心机去编一本字典,但我可能错了。但是,您可以将代码包装在一个函数中,并将
作者
标题
作为参数。

对于
请求
您不必对数据使用
json.dumps
,只需使用
json
参数即可。如果要将响应内容保存到文件,应使用
内容
文本
属性

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)

建议:使用这个库,而不是使用urllib——这会让你的生活变得更加轻松。“data=requests.post(url,json=data).json()”这不起作用。resp=requests.post(url,json=data).json()saveFile=open(“patents.txt”,“w”)saveFile.write(str(resp))saveFile.close()告诉我:value错误:期望值:第1行第1列(char 0)
ValueError
异常意味着
无法解码响应。请确保在
json
参数中传递有效数据。如果你用你当前的代码更新你的帖子,我可以看一下。导入请求导入json title=“computer”author=“Jobs”url=”“data={“q”:{“inventor\u last\u name”:author},{“text\u any”:{“patent\u title”:title}},},“o”:{“仅匹配的子实体”:“true”}var=json.dumps(数据)resp=requests.post(url,data=var)saveFile=open(“patents.txt”,“w”)saveFile.write(str(resp))saveFile.close()saveFile=open(“patents.txt”,“w”)saveFile.write(str(resp))saveFile.close()您键入的
”和“
错误。这会导致500错误响应,这显然不是json响应,因此
.json()
失败。我已经更新了我的帖子,试试看。非常感谢那是一个漫长的夜晚。案件结案!
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)