笔记—mapremote时报错Addon error: Traceback (most recent call last)

KeyError: ‘quote’

  • 源码
def response(flow: http.HTTPFlow):
    if "quote.json" in flow.request.pretty_url:  # 如果url中有quote.json
        res = json.loads(flow.response.content)  # 把返回内容转换为json格式,并赋值给res
        # 把返回内容的data字典中的items字典中的第1个列表中的quote字典中的name字典的值篡改为拼多多mapremote
        res["data"]["items"][0]["quote"]["name"] = "拼多多mapremote"  # 这里语法与jq很相似
        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=……为对象来过滤。

  • 不知道有没有能判断参数的方法,欢迎回帖。