java网络爬虫如何实现 下载本文

内容发布更新时间 : 2024/5/17 19:31:40星期一 下面是文章的全部内容请认真阅读。

八爪鱼·云采集网络爬虫软件

www.bazhuayu.com

更多的Jsoup解析的操作可以参考如下:

1、https://www.ibm.com/developerworks/cn/java/j-lo-jsouphtml/index.html 2、https://jsoup.org/ 一个实例

我们接着上一个爬取数据学习官方网站博客列表的例子讲解一个实例。我们已经知道可以使用Jsoup来解析爬取到的HTML页面内容。那么如何查看我们需要的内容对应的标签呢?以Chrome浏览器为例,我们需要爬取 http://www.datalearner.com/blog_list 这个页面的的博客,首先用Chrome浏览器打开这个网址,然后鼠标右键单击博客的标题,点击“检查”就可以得到HTML页面了。如下图所示。

八爪鱼·云采集网络爬虫软件

www.bazhuayu.com

图2 右键单击标题

八爪鱼·云采集网络爬虫软件

www.bazhuayu.com

图3 点击所在元素的父级元素边上的小三角,收起代码查看

图4 确认当前博客的HTML代码的一致性

通过上述操作之后,我们已经可以看到,所有的博客的标题等信息都存在

class=card的div里面了。于是,我们只要关注这个标签里面的内容是如何组织的,就可以了。如下图所示,我们需要的信息所属的标签,通过点击小三角展开就能得到了。

八爪鱼·云采集网络爬虫软件

www.bazhuayu.com

因此,解析博客列表的代码可以写成如下形式了。 package org.hfutec.example;

import org.apache.http.HttpEntity;import

org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import

org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils;import

八爪鱼·云采集网络爬虫软件

www.bazhuayu.com

org.jsoup.Jsoup;import org.jsoup.nodes.Document;import org.jsoup.nodes.Element;import org.jsoup.select.Elements; import java.io.IOException; /*******

* created by DuFei at 2017.08.25 21:00 * web crawler example * ******/

public class DataLearnerCrawler {

public static void main(String[] args) {

String url = \; String rawHTML = null; try {

rawHTML = getHTMLContent(url); } catch (IOException e) {

e.printStackTrace(); }

//将当前页面转换成Jsoup的Document对象

Document doc = Jsoup.parse(rawHTML);

//获取所有的博客列表集合

Elements blogList = doc.select(\);

//针对每个博客内容进行解析,并输出 for( Element element : blogList ){