如何更正此脚本中的语法和格式?

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

我正在尝试通过将 "f" 参数添加到 GET API 调用来指定所需的字段(参见此处:)。

我认为问题出在我的格式和语法上。

我试过以几种不同的方式向 "q" 和 "f" 参数添加大括号。我 return 不同的错误信息

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)

这就是 returned:

{"status":"error","payload":{"error":"'q' 参数:应该只有一个 json 对象在顶级字典。","code":"RQ3"}}

我希望得到一个包含结果的文件,而不是错误消息。

您的查询数据有误。通过一些更改,它可以工作:

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)

基本上,我把f搬到了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"
     }

您的脚本中存在不平衡的“}”。我纠正了他们。我无法测试更改是否有效。这是更改后的代码。我认为这些是唯一的错误。

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)

请告诉我它是否适合你。