shell 批量判断关键词是否有百度指数

打开文件

#!/bin/bash 
cat filename| whileread line   #filename 为需要读取的文件名,也可以放在命令行参数里。
do
echo$line
done
--------------------------------------
#!/bin/bash
whileread line
do
echo$line#这里可根据实际用途变化
done < filename      #filename 为需要读取的文件名

url

#urlencode
echo -n "汉字语句" | od -t x1 -A n -w1000|tr " ""%"
#urldecode
##暂无

判断返回的内容行数,有指数时返回的行数必然大于某个值

awk 'END{print NR}' a
# 也可以用sed
# sed -n '$=' a

完整脚本

先获取登陆后的cookie,存入cookie.txt,关键词放入文本keywords.txt,以前跑一千个关键词一般没问题,再多就不行了,不知道百度现在的限制是多少了,没有去试过了

#!/bin/sh
# write cookie in file
curl -c ./cookies.txt 'https://passport.baidu.com/v2/api/?login' -H 'Cookie: 略...' --compressed
cat keywords.txt| while read line
do
keyword=`echo -n $line| od -t x1 -A n -w1000|tr " " "%"`
resline=`curl -b ./cookies.txt 'http://index.baidu.com/?tpl=trend&word='$keyword -H 'Accept-Encoding: gzip, deflate, sdch' -H 'Accept-Language: zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4,en-US;q=0.2' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'Referer: http://index.baidu.com/' -H 'Connection: keep-alive' --compressed|sed -n '$='`
if(($resline>500))
then 
result="Yes"
fi
if(($resline<500))
then 
result="No"
fi
echo $result"     "$line"     "$resline
echo $result"     "$line"     "$resline >>result.txt
done
echo "---------------------------game over-----------------------------------"
    原文作者:今夕何夕_walker
    原文地址: https://www.jianshu.com/p/2a7e35b4b69c
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞