KeyError: ‘quote’
def response(flow: http.HTTPFlow):
if "quote.json" in flow.request.pretty_url:
res = json.loads(flow.response.content)
res["data"]["items"][0]["quote"]["name"] = "拼多多mapremote"
res["data"]["items"][1]["quote"]["name"] = "阿里巴巴mapremote"
res["data"]["items"][2]["quote"]["name"] = "京东mapremote"
res["data"]["items"][3]["quote"]["name"] = "中国平安mapremote"
flow.response.text = json.dumps(res)
- 报错信息如下

- 这是因为不止一个请求地址存在quote.json,解决办法就是,让操作的对象唯一,所以源码中第2行代码修改为:
if "/v5/stock/batch/quote.json" in flow.request.pretty_url:
IndexError: list index out of range
- 这时又出现另一个错误,报错信息如下

- 这是因为包含/v5/stock/batch/quote.json的url中,其中一个的items_size=3,而这里篡改了4条记录,所以会有这个报错,解决办法,和前面类似,让操作的对象唯一即可,所以源码的第一层判断下,再加一层判断,代码为:
if "symbol=PDD%2CBABA%2CJD%2CSH601318%2CSZ002400%2CSH600895%2CSH600519%2CSZ000651&extend=detail" in flow.request.pretty_url:
- 说明:
再次过滤,如果url中有symbol=……,因为有多个包的地址都一样,只有参数不一样,且每次获取的地址只有symbol这个参数的值没有变,所以以symbol=……为对象来过滤。