PatentsView API Call

237 views Asked by At

Previously my calls to PatentsView were working. Specifically, I recall running the below code block (from another stackoverflow post) and getting a non-error output. Now I'm getting a 400 error. What might have changed to cause this? Perhaps the requests library? I get a result by pasting the following result in a browser, but cannot replicate with the requests library (even when I reduce the query params to simplify the call). Params f and o are not required, and the "_and" criteria is also not required. You can simply pass q={"_gte":{"patent_date":"2007-01-04"}} after the url and question mark to get a valid result. This is very odd.

https://www.patentsview.org/api/patents/query?q={"_and":[{"_gte":{"patent_date":"2007-01-04"}},{"_lt":{"patent_date":"2007-01-31"}}]}&f=["patent_abstract"]&o={"page":2,"per_page":50}

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)
2

There are 2 answers

0
Roy2012 On

This seems to be working:

import json
from urllib.request import Request, urlopen
import urllib

data = {
    'q':{
        "_and":[
            {"inventor_last_name":author},
            {"_text_any":{"patent_title":title}}
        ]
    }, 
    'o':{"matched_subentities_only": "true"}
}

base_url = "http://www.patentsview.org/api/patents/query"

url = base_url + "?q=" + urllib.parse.quote(json.dumps(data["q"]))

res = urlopen(url)
0
Tibebes. M On

I think you should json.dumps only the values of data.

You can parse it like the following:

from urllib.request import Request, urlopen
from urllib.parse import urlencode
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"}
}

data = dict(map(lambda x: (x[0], json.dumps(x[1])), data.items() ))

resp = urlopen(Request(url +'?' + urlencode(data)))
print(resp.read())

This should solve the issue