-
随笔记录
某站点对请求参数加密的时候对键值对都进行了encodeURIComponent。那encodeURIComponent的作用是什么呢?如何在python中进行使用呢?使用过程中如何结合实际情况解决问题呢?以下是避坑指南,会让你少走很多弯路。
-
作用
encodeURIComponent用于将字符串中的特殊字符进行编码,使其成为符合 URI 规范的安全字符,以便于在 URI 中进行传输和解析。例如在 URI 中,有一些特殊字符具有特殊的含义,例如 (:,/,?,#) 等,如果 URI 中包含这些特殊字符,并且它们不是作为字符的一部分出现(例如在查询参数中),就需要对它们进行编码,以免与 URI 的语义产生冲突。
-
js举例实战
-
python举例实战
前面在js中举例了encodeURIComponent如何使用,那在python中如何使用呢?其时python的库中有一个urllib的parse的quote方法,可以将一些特殊字符进行编码
如:
from urllib.parse import quote char_list = [":","?","/","#"," ",'"'] for char in char_list: print("原字符:",char,"编码后字符:",quote(char))
大家可能有个疑问,为啥"/"没有被编码呢?
这里就是我们今天说的重点,要是大家平时没注意的话,可能很久找不出原因,可能是对api不熟悉吧,哈哈。
我们进入quote方法内部查看,他提供了四个参数如下:
def quote(string, safe='/', encoding=None, errors=None): pass
-
string
是需要被编码的字符串
-
safe
参数的默认值为 ‘/’。该字符是保留的,但在典型用法中,quote 函数被调用在保留现有斜杠字符的路径上,这就解释了前面我们举例当中,为啥"/"没有被编码的原因了
-
encoding和errors
可选的 encoding 和 errors 参数指定如何处理非 ASCII 字符,方法与 str.encode 方法接受的方式相同。
默认情况下,encoding=‘utf-8’(字符使用 UTF-8 编码),errors=‘strict’(不支持的字符会引发 UnicodeEncodeError)
-
-
网站参数编码实战
某站的请求参数之一例如:
dm_img_inter: {"ds":[{"t":2,"c":"Y2xlYXJmaXggZy1zZWFyY2ggc2VhcmNoLWNvbnRhaW5lcg","p":[1380,38,735],"s":[183,645,782]},{"t":2,"c":"d3JhcHBlcg","p":[825,59,1355],"s":[406,4385,3864]}],"wh":[3303,1736,55],"of":[293,586,293]}
而URL上带的是:
dm_img_inter=%7B%22ds%22:[%7B%22t%22:2,%22c%22:%22Y2xlYXJmaXggZy1zZWFyY2ggc2VhcmNoLWNvbnRhaW5lcg%22,%22p%22:[1380,38,735],%22s%22:[183,645,782]%7D,%7B%22t%22:2,%22c%22:%22d3JhcHBlcg%22,%22p%22:[825,59,1355],%22s%22:[406,4385,3864]%7D],%22wh%22:[3303,1736,55],%22of%22:[293,586,293]%7D
我们直接使用quote对dm_img_inter的值进行编码,记得将字典序列化成字符串,使用json.dumps
import json from urllib.parse import quote dm_img_inter = {"ds":[{"t":2,"c":"Y2xlYXJmaXggZy1zZWFyY2ggc2VhcmNoLWNvbnRhaW5lcg","p":[1380,38,735],"s":[183,645,782]},{"t":2,"c":"d3JhcHBlcg","p":[825,59,1355],"s":[406,4385,3864]}],"wh":[3303,1736,55],"of":[293,586,293]} dm_img_inter_str = json.dumps(dm_img_inter,separators=(",","")) print(quote(dm_img_inter_str)) # 目标 #%7B%22ds%22:[%7B%22t%22:2,%22c%22:%22Y2xlYXJmaXggZy1zZWFyY2ggc2VhcmNoLWNvbnRhaW5lcg%22,%22p%22:[1380,38,735],%22s%22:[183,645,782]%7D,%7B%22t%22:2,%22c%22:%22d3JhcHBlcg%22,%22p%22:[825,59,1355],%22s%22:[406,4385,3864]%7D],%22wh%22:[3303,1736,55],%22of%22:[293,586,293]%7D # 结果 #%7B%22ds%22%3A%5B%7B%22t%22%3A2%2C%22c%22%3A%22Y2xlYXJmaXggZy1zZWFyY2ggc2VhcmNoLWNvbnRhaW5lcg%22%2C%22p%22%3A%5B1380%2C38%2C735%5D%2C%22s%22%3A%5B183%2C645%2C782%5D%7D%2C%7B%22t%22%3A2%2C%22c%22%3A%22d3JhcHBlcg%22%2C%22p%22%3A%5B825%2C59%2C1355%5D%2C%22s%22%3A%5B406%2C4385%2C3864%5D%7D%5D%2C%22wh%22%3A%5B3303%2C1736%2C55%5D%2C%22of%22%3A%5B293%2C586%2C293%5D%7D
你这样编码以后去发请求,肯定是不对的,经过细心观察以后,我们发现【: , [ ]】这四个字符是不会被编码的
我们刚刚利用前面说的一个知识点,safe参数,将这四个字符设置进去
import json from urllib.parse import quote dm_img_inter = {"ds":[{"t":2,"c":"Y2xlYXJmaXggZy1zZWFyY2ggc2VhcmNoLWNvbnRhaW5lcg","p":[1380,38,735],"s":[183,645,782]},{"t":2,"c":"d3JhcHBlcg","p":[825,59,1355],"s":[406,4385,3864]}],"wh":[3303,1736,55],"of":[293,586,293]} dm_img_inter_str = json.dumps(dm_img_inter,separators=(",","")) print(quote(dm_img_inter_str, safe=':[],')) # 目标 #%7B%22ds%22:[%7B%22t%22:2,%22c%22:%22Y2xlYXJmaXggZy1zZWFyY2ggc2VhcmNoLWNvbnRhaW5lcg%22,%22p%22:[1380,38,735],%22s%22:[183,645,782]%7D,%7B%22t%22:2,%22c%22:%22d3JhcHBlcg%22,%22p%22:[825,59,1355],%22s%22:[406,4385,3864]%7D],%22wh%22:[3303,1736,55],%22of%22:[293,586,293]%7D # 结果 #%7B%22ds%22:[%7B%22t%22:2,%22c%22:%22Y2xlYXJmaXggZy1zZWFyY2ggc2VhcmNoLWNvbnRhaW5lcg%22,%22p%22:[1380,38,735],%22s%22:[183,645,782]%7D,%7B%22t%22:2,%22c%22:%22d3JhcHBlcg%22,%22p%22:[825,59,1355],%22s%22:[406,4385,3864]%7D],%22wh%22:[3303,1736,55],%22of%22:[293,586,293]%7D
现在就完全一样了,简直perfect!
-
总结
需要多熟悉使用的api,并且需要和目标站点保持一直,方能成功!