zoukankan      html  css  js  c++  java
  • java爬虫简单示例

    此文是为方便有一定基础的小白看的java爬虫流程,欢迎指正!

    github链接  https://github.com/geekWastelands/java/tree/master/src/TianJin

    一、流程图


    二、根据流程写程序(以抓取天津市卫生厅数据(通知、公告、新闻)为例)
    1.目录结构


    2.主函数


    截图1


    3.如何定位到自己需要的信息

    附上jsoup中文文档 http://www.open-open.com/jsoup/

    不过这只能定位到一个网址 我们的目的是得到页面所有需要的网址如


    可以在刚刚的路径上进行修改
    刚刚copy selector的路径
    body > table:nth-child(12) > tbody > tr > td:nth-child(3) > table > tbody > tr > td > table:nth-child(2) > tbody > tr > td:nth-child(2) > table:nth-child(1) > tbody > tr > td:nth-child(1) > a
    经过对每一个url所在位置的分析
    比如:
    body > table:nth-child(12) > tbody > tr > td:nth-child(3) > table > tbody > tr > td > table:nth-child(2) > tbody > tr > td:nth-child(2) > table:nth-child(2) > tbody > tr > td:nth-child(1) > a
    再比如:
    body > table:nth-child(12) > tbody > tr > td:nth-child(3) > table > tbody > tr > td > table:nth-child(2) > tbody > tr > td:nth-child(2) > table:nth-child(3) > tbody > tr > td:nth-child(1) > a
    发现规律
    body > table:nth-child(12) > tbody > tr > td:nth-child(3) > table > tbody > tr > td > table:nth-child(2) > tbody > tr > td:nth-child(2) > table> tbody > tr > td:nth-child(1) > a
    该路径可以获取到页面的所有所需的url
    可以粘贴按enter键试试看


    最后遍历这些url并获取源码来筛选更多我们需要的信息


    比如标题时间内容文章源码等等
    4.最后把get到的信息封装一下调用插入数据库函数即可
    ps:不同的网站可能会有不同的问题 比如post请求(给的例子是get请求不同之处请谷歌之)、ajax异步请求等等 遇到的时候再详细讨论

  • 相关阅读:
    vsftpd 启动 vsftpd:500 OOPS: bad bool value in config file for: guest_enable
    Vsftpd服务传输文件(转)
    搭建FTP服务
    sed命令
    创建服务类脚本
    jvm 方法区
    B+与B-树
    适配器模式
    java 垃圾回收总结(可达性分析 引用分类
    HBase常见问题答疑解惑【持续更新中】
  • 原文地址:https://www.cnblogs.com/wastedlands/p/9310001.html
Copyright © 2011-2022 走看看