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

 

posted on 2018-07-14 16:19  wastelands  阅读(785)  评论(0编辑  收藏  举报

导航