抓取WordPress博客全部文章链接生成Sitemap.xml的Shell脚本代码

2019年10月21日

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

执行脚本的方法

 bash baidu-sitemap.sh https://blog.com
    OR
sh baidu-sitemap.sh https://blog.com
    OR
chmod 777 baidu-sitemap.sh
./baidu-sitemap.sh https://blog.com
#脚本后边必须跟随着您要抓取的网站,不能以/结尾

 

完整的baidu脚本代码

#! /bin/bash
#This WordPress baidu Sitemap tools
#autor:RucLinux
#web: https://www.myzhenai.com.cn https://jiayu.mybabya.com https://mangshe.python.hn.cn

url="${1}"
phat="/home/sitemap.xml"
xml="< ?xml version=\"1.0\" encoding=\"utf-8\"?>"
st=""
ul="    "
lo="        ${url}"
lc=""
mod="        "
tmod=""
req="        "
dai="daily"
freq=""
pri="        0.8"
ur="    "
se=""
ni=$(date +20%y-%m-%d)
x=0
  if [ ! -f "${phat}" ]; then
    echo -e "${xml}\n${st}" > ${phat}
  fi
  if [ ! -n "${1}" ]; then
    echo "error! Please enter the domain name you want to enumerate"
  else
    sed -i "/< \/urlset>/d" ${phat}
    page=$(curl -s ${1}|grep \/post\/date\/|awk -F "'" '{ print $2 }'|awk -F "'" '{ print $1 }')

    arr=(${page})
      for i in ${arr[@]}
      do

        loc=$(curl -s ${i}|grep -Eo \/post\/'[0-9]+'.html)
        #sleep 10s

                #这里再进一次循环
        uarr=(${loc})
        for ii in ${uarr[@]}
        do
               #在这里对所获取的页面里的网址与sitemap.xml里的网址进行对比,如果没有再写入,如果存在就抛弃
           pan=$(cat ${phat}|grep "${ii}")
           if [[ ${pan} == "" ]]; then
           echo -e "${ul}\n${lo}${ii}${lc}\n${mod}${ni}${tmod}\n${req}${dai}${freq}\n${pri}\n${ur}" >> ${phat}
#           x++
#${st}参数要写在输入的前边, ${se}参数要写在结尾。
           fi
        done
      done
      echo "${se}" >> ${phat}
  fi

 

Google版本完整源码

#! /bin/bash
#This WordPress Google Sitemap tools
#autor:RucLinux
#web: https://www.myzhenai.com.cn https://jiayu.mybabya.com https://mangshe.python.hn.cn

url="${1}"
phat="/home/sitemap.txt"
lo="${url}"
if [ ! -n "${1}" ]; then
  echo "error! Please enter the domain name you want to enumerate"
else
  page=$(curl -s ${1}|grep \/post\/date\/|awk -F "'" '{ print $2 }'|awk -F "'" '{ print $1 }')
  arr=(${page})
    for i in ${arr[@]}
    do
        loc=$(curl -s ${i}|grep -Eo \/post\/'[0-9]+'.html)
  uarr=(${loc})
        for ii in ${uarr[@]}
        do
            pan=$(cat ${phat}|grep "${ii}")
            if [[ ${pan} == "" ]]; then
                echo -e "${lo}${ii}" >> ${phat}
            fi
        done
    done
fi

 


sicnature ---------------------------------------------------------------------
Your current IP address is: 54.144.95.36
Your IP address location: 美国弗吉尼亚阿什本
Your IP address country and region: 美国 美国
Your current browser is:
Your current system is:
Original content, please indicate the source:
同福客栈论坛 | 蟒蛇科普海南乡情论坛 | JiaYu Blog
sicnature ---------------------------------------------------------------------
Welcome to reprint. Please indicate the source https://www.myzhenai.com.cn/post/2871.html

1 评论

发表回复

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