熬夜写个实用的小脚本-抓取wordpress博客文章地址
Tofloor
poster avatar
RucLinux
deepin
2018-02-10 12:48
Author
原文地址:http://www.myzhenai.com.cn/post/2994.html http://www.myzhenai.com/thread-18082-1-1.html

做为一个个人站长,经常要为博客或网站的收录头疼,我也是经常发现我的博客百度并没有收录,于是我依照百度站长上的sitemap.xml的格式,自己写了一个Shell脚本来抓取博客文章地址并自动生成一个sitemap.xml文件,只要将这个文件提交给百度站长,并修改这个脚本把这个脚本放在服务器上的crontab里就可以自动生成了。这个脚本的思路很简单,通过读取Archives里的页面地址进行分析和抓取这些页面上的文章链接。并将这些链接通这脚本写入到sitemap.xml文件里边去。

执行脚本的方法
  1. bash baidu-sitemap.sh http://blog.com
  2. OR
  3. sh baidu-sitemap.sh http://blog.com
  4. OR
  5. chmod 777 baidu-sitemap.sh
  6. ./baidu-sitemap.sh http://blog.com
  7. #脚本后边必须跟随着您要抓取的网站,不能以/结尾
Copy the Code

完整脚本代码
  1. #! /bin/bash
  2. #This WordPress baidu Sitemap tools
  3. #autor:RucLinux
  4. #web: http://www.myzhenai.com.cn http://jiayu.mybabya.com

  5. url="${1}"
  6. phat="/home/RucLinux/sitemap.xml"
  7. xml=""
  8. st=""
  9. ul="    "
  10. lo="        ${url}"
  11. lc=""
  12. mod="        "
  13. tmod=""
  14. req="        "
  15. dai="daily"
  16. freq=""
  17. pri="        0.8"
  18. ur="    "
  19. se=""
  20. ni=$(date +20%y-%m-%d)
  21. if [ ! -f "${phat}" ]; then
  22.   echo -e "${xml}\n${st}" > ${phat}
  23. fi
  24. if [ ! -n "${1}" ]; then
  25.   echo "error! Please enter the domain name you want to enumerate"
  26. else
  27.   sed -i "/<\/urlset>/d" ${phat}
  28.   page=$(curl -s ${1}|grep \/post\/date\/|awk -F "'" '{ print $2 }'|awk -F "'" '{ print $1 }')
  29.   arr=(${page})
  30.     for i in ${arr[@]}
  31.     do
  32.         loc=$(curl -s ${i}|grep -Eo \/post\/'[0-9]+'.html)
  33.   uarr=(${loc})
  34.         for ii in ${uarr[@]}
  35.         do
  36.             pan=$(cat ${phat}|grep "${ii}")
  37.             if [[ ${pan} == "" ]]; then
  38.                 echo -e "${ul}\n${lo}${ii}${lc}\n${mod}${ni}${tmod}\n${req}${dai}${freq}\n${pri}\n${ur}" >> ${phat}
  39.             fi
  40.         done
  41.     done
  42.   echo "${se}" >> ${phat}
  43. fi
Copy the Code
脚本代码解释
  1. #! /bin/bash
  2. #This WordPress baidu Sitemap tools
  3. #autor:RucLinux
  4. #web: http://www.myzhenai.com.cn http://jiayu.mybabya.com
  5. #以上这些是解释器和脚本作用、版权信息注释

  6. url="${1}"
  7. #将运行脚本时输入的网址传参给url变量
  8. phat="/home/RucLinux/sitemap.xml"
  9. #指定sitemap.xml的路径和文件名
  10. xml=""
  11. st=""
  12. ul="    "
  13. lo="        ${url}"
  14. lc=""
  15. mod="        "
  16. tmod=""
  17. req="        "
  18. dai="daily"
  19. freq=""
  20. pri="        0.8"
  21. ur="    "
  22. se=""
  23. # xml st ul lo lc mod tmod req dai freq pri ur se 这些都是特意指定的文本变量,需要把这些和抓取到的网址一起写入到文件中。
  24. ni=$(date +20%y-%m-%d)
  25. #获取当前时间,年-月-日
  26. if [ ! -f "${phat}" ]; then
  27.   echo -e "${xml}\n${st}" > ${phat}
  28. fi
  29. #以上这个判断是识别sitemap.xml文件是否存在,如果不存在这创建一个。
  30. if [ ! -n "${1}" ]; then
  31.   echo "error! Please enter the domain name you want to enumerate"
  32.    #上边这一行是用户在执行脚本的时候没有输入网址会提示的错误信息。
  33. else
  34.   sed -i "/<\/urlset>/d" ${phat}
  35.     #如果sitemap.xml文件存在,并且有内容的时候,先把文件尾的标签删除。
  36.   page=$(curl -s ${1}|grep \/post\/date\/|awk -F "'" '{ print $2 }'|awk -F "'" '{ print $1 }')
  37.     #读取用户执行脚本时输入的网址源代码,用/post/date/做为搜索关键词,将所有月份的文章存档页面地址读取出来,如果要修改抓取其他地址的话,将这一行的关键词修改就可以了。
  38.   arr=(${page})
  39.     将所有内容存入一个数组里
  40.     for i in ${arr[@]}
  41.     do
  42.         #进入数组循环
  43.         loc=$(curl -s ${i}|grep -Eo \/post\/'[0-9]+'.html)
  44.                 #读取文章存档日期页面并以/post/xxx(xxx为纯数字).html为关键词进行搜索,抓取并返回所有匹配的链接。
  45.     uarr=(${loc})
  46.         #第二次进入数组,第一个数组循环的是所有文章存档日期页面,这个数组保存的是单独一个页面里所有符合条件的文章链接。
  47.         for ii in ${uarr[@]}
  48.         do
  49.                 #第二次循环,这次是将在单独页面里抓取到的文章链接与sitemap.xml里的比照判断。
  50.             pan=$(cat ${phat}|grep "${ii}")
  51.                         #用$ii这个变量内容去sitemap.xml里边查找看有没有符合的,$ii的内容即是不同的/post/xxx.html,如果查找后没找到说明还没有写入,就执行下一步,否则跳过这个关键词
  52.             if [[ ${pan} == "" ]]; then
  53.                 echo -e "${ul}\n${lo}${ii}${lc}\n${mod}${ni}${tmod}\n${req}${dai}${freq}\n${pri}\n${ur}" >> ${phat}
  54.             fi
  55.                         #这个判断条件是在sitemap.xml里没有找到与$ii相同内容的链接地址,存在的话就会自动跳过。
  56.         done
  57.     done
  58.   echo "${se}" >> ${phat}
  59.     #前边我们的代码删除了一个标签,脚本执行到这里是已经完成所有操作了,所以我们要让脚本在sitemap.xml文件尾端添加一个标签。
  60. fi
Copy the Code







Reply Favorite View the author
All Replies
avatar
lulinux
deepin
2018-02-10 13:09
#1
要用高质量内容“感化”百度蜘蛛。
Reply View the author
avatar
RucLinux
deepin
2018-02-10 13:26
#2
https://bbs.deepin.org/post/153118
要用高质量内容“感化”百度蜘蛛。

我对SEO不是太关注,也不懂。所以只有这样。
Reply View the author
avatar
RucLinux
deepin
2018-02-10 13:28
#3
https://bbs.deepin.org/post/153118
要用高质量内容“感化”百度蜘蛛。

刚才把sitemap提交给百度了,显示是百度蜘蛛抓取了几百条。
Reply View the author
avatar
lulinux
deepin
2018-02-10 14:09
#4
哈哈。你不会还没睡吧。
Reply View the author