巧用正则+Chrome彩蛋功能=快速生成cUrl脚本 fetch、cUrl cmd、cUrl bash、PowerShell

 Yuema约吗?一起学技术,一起成长!学海无涯 高人带路系列

程序的世界,就是有坑的地方!分享踩坑的心得与体验!每天分享一点点!
关注公众号,进入学海无涯,高人带路模式!!学会一招神技能​

今天遇到一个需要批量调接口的场景,从数据库中查出上万条数据,再按接口规则生成带get参数的url请求,还要带登陆状态。

一、从数据库中捞出数据

将查出来的数据贴到notepad++中,大概是这样子的数据,示范一下

1  a  112  b  223  c  334  d  445  e  556  f  667  g  778  h  889  i  99

将空格换成%20,这个直接把空格换掉就是,示例数据没有演示这个场景,自行脑补一下。

二、使用chrome生成一个带cookie的请求

操作截图如上图所示,是不是很方便,cookies都带上了,有登陆状态,调接口相当方面。

curl "http://yue.ma/?i=9&m=6&p=11" -H "Connection: keep-alive" -H "Cache-Control: max-age=0" -H "Upgrade-Insecure-Requests: 1" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3" -H "Accept-Encoding: gzip, deflate" -H "Accept-Language: zh-CN,zh;q=0.9" -H "Cookie: __51cke__=; __tins__18251311=^%^7B^%^22sid^%^22^%^3A^%^201575548726427^%^2C^%^20^%^22vd^%^22^%^3A^%^203^%^2C^%^20^%^22expires^%^22^%^3A^%^201575551216664^%^7D; __51laig__=3" --compressed --insecure

三、使用正则替换,生成批量脚本

制作一个脚本正则模板

curl "http://yue.ma/?i=$1&m=$2&p=$3" -H "Connection: keep-alive" -H "Cache-Control: max-age=0" -H "Upgrade-Insecure-Requests: 1" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3" -H "Accept-Encoding: gzip, deflate" -H "Accept-Language: zh-CN,zh;q=0.9" -H "Cookie: __51cke__=; __tins__18251311=^%^7B^%^22sid^%^22^%^3A^%^201575548726427^%^2C^%^20^%^22vd^%^22^%^3A^%^203^%^2C^%^20^%^22expires^%^22^%^3A^%^201575551216664^%^7D; __51laig__=3" --compressed --insecure

使用notepad++替换功能

查找目标填写:

^([^\t]+)\t([^\t]+)\t([^\t]+)$

替换为填写:

curl "http://yue.ma/?i=$1&m=$2&p=$3" -H "Connection: keep-alive" -H "Cache-Control: max-age=0" -H "Upgrade-Insecure-Requests: 1" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3" -H "Accept-Encoding: gzip, deflate" -H "Accept-Language: zh-CN,zh;q=0.9" -H "Cookie: __51cke__=; __tins__18251311=^%^7B^%^22sid^%^22^%^3A^%^201575548726427^%^2C^%^20^%^22vd^%^22^%^3A^%^203^%^2C^%^20^%^22expires^%^22^%^3A^%^201575551216664^%^7D; __51laig__=3" --compressed --insecure

生成脚本

curl "http://yue.ma/?i=1&m=a&p=11" -H "Connection: keep-alive" -H "Cache-Control: max-age=0" -H "Upgrade-Insecure-Requests: 1" -H "User-Agent: Mozilla/5.0 Windows NT 10.0; Win64; x64 AppleWebKit/537.36 KHTML, like Gecko Chrome/78.0.3904.108 Safari/537.36" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3" -H "Accept-Encoding: gzip, deflate" -H "Accept-Language: zh-CN,zh;q=0.9" -H "Cookie: __51cke__=; __tins__18251311=^%^7B^%^22sid^%^22^%^3A^%^201575548726427^%^2C^%^20^%^22vd^%^22^%^3A^%^203^%^2C^%^20^%^22expires^%^22^%^3A^%^201575551216664^%^7D; __51laig__=3" --compressed --insecurecurl "http://yue.ma/?i=2&m=b&p=22" -H "Connection: keep-alive" -H "Cache-Control: max-age=0" -H "Upgrade-Insecure-Requests: 1" -H "User-Agent: Mozilla/5.0 Windows NT 10.0; Win64; x64 AppleWebKit/537.36 KHTML, like Gecko Chrome/78.0.3904.108 Safari/537.36" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3" -H "Accept-Encoding: gzip, deflate" -H "Accept-Language: zh-CN,zh;q=0.9" -H "Cookie: __51cke__=; __tins__18251311=^%^7B^%^22sid^%^22^%^3A^%^201575548726427^%^2C^%^20^%^22vd^%^22^%^3A^%^203^%^2C^%^20^%^22expires^%^22^%^3A^%^201575551216664^%^7D; __51laig__=3" --compressed --insecurecurl "http://yue.ma/?i=3&m=c&p=33" -H "Connection: keep-alive" -H "Cache-Control: max-age=0" -H "Upgrade-Insecure-Requests: 1" -H "User-Agent: Mozilla/5.0 Windows NT 10.0; Win64; x64 AppleWebKit/537.36 KHTML, like Gecko Chrome/78.0.3904.108 Safari/537.36" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3" -H "Accept-Encoding: gzip, deflate" -H "Accept-Language: zh-CN,zh;q=0.9" -H "Cookie: __51cke__=; __tins__18251311=^%^7B^%^22sid^%^22^%^3A^%^201575548726427^%^2C^%^20^%^22vd^%^22^%^3A^%^203^%^2C^%^20^%^22expires^%^22^%^3A^%^201575551216664^%^7D; __51laig__=3" --compressed --insecurecurl "http://yue.ma/?i=4&m=d&p=44" -H "Connection: keep-alive" -H "Cache-Control: max-age=0" -H "Upgrade-Insecure-Requests: 1" -H "User-Agent: Mozilla/5.0 Windows NT 10.0; Win64; x64 AppleWebKit/537.36 KHTML, like Gecko Chrome/78.0.3904.108 Safari/537.36" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3" -H "Accept-Encoding: gzip, deflate" -H "Accept-Language: zh-CN,zh;q=0.9" -H "Cookie: __51cke__=; __tins__18251311=^%^7B^%^22sid^%^22^%^3A^%^201575548726427^%^2C^%^20^%^22vd^%^22^%^3A^%^203^%^2C^%^20^%^22expires^%^22^%^3A^%^201575551216664^%^7D; __51laig__=3" --compressed --insecurecurl "http://yue.ma/?i=5&m=e&p=55" -H "Connection: keep-alive" -H "Cache-Control: max-age=0" -H "Upgrade-Insecure-Requests: 1" -H "User-Agent: Mozilla/5.0 Windows NT 10.0; Win64; x64 AppleWebKit/537.36 KHTML, like Gecko Chrome/78.0.3904.108 Safari/537.36" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3" -H "Accept-Encoding: gzip, deflate" -H "Accept-Language: zh-CN,zh;q=0.9" -H "Cookie: __51cke__=; __tins__18251311=^%^7B^%^22sid^%^22^%^3A^%^201575548726427^%^2C^%^20^%^22vd^%^22^%^3A^%^203^%^2C^%^20^%^22expires^%^22^%^3A^%^201575551216664^%^7D; __51laig__=3" --compressed --insecurecurl "http://yue.ma/?i=6&m=f&p=66" -H "Connection: keep-alive" -H "Cache-Control: max-age=0" -H "Upgrade-Insecure-Requests: 1" -H "User-Agent: Mozilla/5.0 Windows NT 10.0; Win64; x64 AppleWebKit/537.36 KHTML, like Gecko Chrome/78.0.3904.108 Safari/537.36" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3" -H "Accept-Encoding: gzip, deflate" -H "Accept-Language: zh-CN,zh;q=0.9" -H "Cookie: __51cke__=; __tins__18251311=^%^7B^%^22sid^%^22^%^3A^%^201575548726427^%^2C^%^20^%^22vd^%^22^%^3A^%^203^%^2C^%^20^%^22expires^%^22^%^3A^%^201575551216664^%^7D; __51laig__=3" --compressed --insecurecurl "http://yue.ma/?i=7&m=g&p=77" -H "Connection: keep-alive" -H "Cache-Control: max-age=0" -H "Upgrade-Insecure-Requests: 1" -H "User-Agent: Mozilla/5.0 Windows NT 10.0; Win64; x64 AppleWebKit/537.36 KHTML, like Gecko Chrome/78.0.3904.108 Safari/537.36" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3" -H "Accept-Encoding: gzip, deflate" -H "Accept-Language: zh-CN,zh;q=0.9" -H "Cookie: __51cke__=; __tins__18251311=^%^7B^%^22sid^%^22^%^3A^%^201575548726427^%^2C^%^20^%^22vd^%^22^%^3A^%^203^%^2C^%^20^%^22expires^%^22^%^3A^%^201575551216664^%^7D; __51laig__=3" --compressed --insecurecurl "http://yue.ma/?i=8&m=h&p=88" -H "Connection: keep-alive" -H "Cache-Control: max-age=0" -H "Upgrade-Insecure-Requests: 1" -H "User-Agent: Mozilla/5.0 Windows NT 10.0; Win64; x64 AppleWebKit/537.36 KHTML, like Gecko Chrome/78.0.3904.108 Safari/537.36" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3" -H "Accept-Encoding: gzip, deflate" -H "Accept-Language: zh-CN,zh;q=0.9" -H "Cookie: __51cke__=; __tins__18251311=^%^7B^%^22sid^%^22^%^3A^%^201575548726427^%^2C^%^20^%^22vd^%^22^%^3A^%^203^%^2C^%^20^%^22expires^%^22^%^3A^%^201575551216664^%^7D; __51laig__=3" --compressed --insecurecurl "http://yue.ma/?i=9&m=i&p=99" -H "Connection: keep-alive" -H "Cache-Control: max-age=0" -H "Upgrade-Insecure-Requests: 1" -H "User-Agent: Mozilla/5.0 Windows NT 10.0; Win64; x64 AppleWebKit/537.36 KHTML, like Gecko Chrome/78.0.3904.108 Safari/537.36" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3" -H "Accept-Encoding: gzip, deflate" -H "Accept-Language: zh-CN,zh;q=0.9" -H "Cookie: __51cke__=; __tins__18251311=^%^7B^%^22sid^%^22^%^3A^%^201575548726427^%^2C^%^20^%^22vd^%^22^%^3A^%^203^%^2C^%^20^%^22expires^%^22^%^3A^%^201575551216664^%^7D; __51laig__=3" --compressed --insecure
执行脚本,我使用git bash执行的,直接新建一个bash.sh,把生成的代码放进去,执行脚本
./bash.sh
等着几万条调用执行吧!no no no,数据过多,可以拆成多份bash脚本执行,可以加快速度。这种执行方式有一个好处就是,不需要额外去写程序,直接一顿replace生成脚本跑起来。不好的就是命令太多的话,执行并没有进度显示。
可以写个辅助的脚本执行工具,自动拆份脚本并行执行,显示显示进度什么的。嗯嗯,学会一下^([^\t]+)\t([^\t]+)\t([^\t]+)$ 与chrome的脚本生成功能,非常不错!

作者:钟代麒

出处:http://www.jishudao.com/
版权归作者所有,转载请注明出处

此条目发表在未分类分类目录。将固定链接加入收藏夹。

发表评论

邮箱地址不会被公开。 必填项已用*标注