How do I correct the syntax and formatting in this script?

96 views Asked by At

I am trying to specify the fields needed by adding the "f" argument to the GET API call (see here: Patentsview API Python 3.4).

I believe the problem is my formatting and syntax.

I've tried adding curly brackets in several different ways to the "q" and "f" arguments. I return different error messages

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}}], 
    "f":["assignee_lastknown_city","assignee_lastknown_state","assignee_lastknown_country"]},
    "o":{"matched_subentities_only":"true"}
} 
resp = requests.post(url, json=data) 
with open("patents.txt", "w") as f:
    f.write(resp.text)

This is what is returned:

{"status":"error","payload":{"error":"'q' parameter: should only have one json object in the top-level dictionary.","code":"RQ3"}}

I would expect a file with results, not an error message.

2

There are 2 answers

1
Amit On BEST ANSWER

There were unbalanced '}' in your script. I corrected them. I have not been able to test whether the changes worked or not. Here is the changed code. I have assumed that those were the only errors.

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}}]},"f":["assignee_lastknown_city","assignee_lastknown_state","assignee_lastknown_country"],"o":{"matched_subentities_only":"true"}} 
resp = requests.post(url, json=data) 
with open("patents.txt", "w") as f:
    f.write(resp.text)

Please let me know if it worked for you.

0
AudioBubble On

You have an error in your query data. With a few changes, it works:

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
                }
            }
        ],
    },
    "f": [
        "assignee_lastknown_city",
        "assignee_lastknown_state",
        "assignee_lastknown_country"
    ],
    "o": {
        "matched_subentities_only": "true"
    }
}
resp = requests.post(url, json=data)
with open("patents.txt", "w") as f:
    f.write(resp.text)

Basically, I moved f outside q:

--- op_data.txt 2019-07-28 18:37:07.000000000 -0400
+++ my_data.txt 2019-07-28 18:37:07.000000000 -0400
@@ -9,13 +9,13 @@
                     "patent_title": "computer"
                 }
             }
-        ],
-        "f": [
-            "assignee_lastknown_city",
-            "assignee_lastknown_state",
-            "assignee_lastknown_country"
         ]
     },
+    "f": [
+        "assignee_lastknown_city",
+        "assignee_lastknown_state",
+        "assignee_lastknown_country"
+    ],
     "o": {
         "matched_subentities_only": "true"
     }