zoukankan      html  css  js  c++  java
  • shell爬虫--抓取某在线文档所有页面

    在线教程一般像流水线一样,页面有上一页下一页的按钮,因此,可以利用shell写一个爬虫读取下一页链接地址,配合wget将教程所有内容抓取。

    以postgresql中文网为例。下面是实例代码

    #!/bin/sh
    start_URL="http://www.postgres.cn/docs/9.6/preface.html"
    end_URL="http://www.postgres.cn/docs/9.6/bookindex.html"
    URL=$start_URL
    
    while [ $URL != $end_URL ];do
    
    curl -s  $URL >tmp.txt
    wget $URL -P psql
    grep -n 'ACCESSKEY="N"'  tmp.txt > tmp2.txt
    cut -f1 -d":" tmp2.txt | head -n 1 > tmp3.txt
    let LINE=`cat tmp3.txt`
    let LINE--
    sed -n "${LINE}p" tmp.txt > tmp4.txt
    sed -i 's/HREF="//g' tmp4.txt
    sed -i 's/"//g' tmp4.txt
    sURL=`cat tmp4.txt`
    cat tmp4.txt >> allurl.txt
    FULLURL="http://www.postgres.cn/docs/9.6/$sURL"
    URL=$FULLURL
    
    done
    
    rm -rf tmp.txt tmp2.txt tmp3.txt tmp4.txt

     说明:

    1、URL 要下载的html文件路径

    2、sURL html文件的相对路径

    3、FULLURL  sURL和模板拼接后的完整url

    4、tmp.txt  用于保存curl取得的页面数据

  • 相关阅读:
    redis
    装饰器之functools与before_request
    版本
    git常用命令
    支付宝支付示例
    ContentType
    vue的基础使用
    es6简单介绍
    解析器、路由控制、分页与响应器
    元素水平居中的方法
  • 原文地址:https://www.cnblogs.com/sherlock-merlin/p/9041864.html
Copyright © 2011-2022 走看看