java爬虫是_java爬虫简单示例

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

一、流程图

b9815a1200a47b10e74548b464fd763a.png

7682a26c220ea94614e072e8dd97884f.png

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

1.目录结构

fe0f0fddfd99c54467c0f8d7f8ec2126.png

2.主函数

d7d57cd08655c4b9db445508a5eed8bd.png

截图1

d748cd0f0fb918cd053e32a7ba15494b.png

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

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

f8bd0017da7506c1efbe0002522db9b1.png

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

3d726225195e0c0f0f50f16bca16ee75.png

可以在刚刚的路径上进行修改

刚刚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键试试看

a5637a9f79d4127ae19f658976a8e28d.png

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

1751b3313971b11eff32b7c1d27d7539.png

比如标题时间内容文章源码等等

4.最后把get到的信息封装一下调用插入数据库函数即可

ps:不同的网站可能会有不同的问题 比如post请求(给的例子是get请求不同之处请谷歌之)、ajax异步请求等等 遇到的时候再详细讨论

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值