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异步请求等等 遇到的时候再详细讨论

  • 相关阅读:
    Sql例子Sp_ExecuteSql 带参数
    Flex显示麦克风当前音量
    无法将 flash.display::Sprite@156b7b1 转换为 mx.core.IUIComponent
    FMS (端口问题)如何穿透防火墙
    19:A*B问题
    6264:走出迷宫
    2753:走迷宫
    1792:迷宫
    换钱问题(经典枚举样例)
    1943(2.1)
  • 原文地址:https://www.cnblogs.com/wastedlands/p/9310001.html
Copyright © 2011-2022 走看看