Android学习——Jsoup实现网络爬虫,爬取贤集网

前言

因项目需要,尝试把网站的新闻给爬下来,因为是安卓端,所以这里采用了Jsoup库。
感谢:https://blog.csdn.net/qq_30379689/article/details/55005796
关于Jsoup的基本操作,大家可以去这篇博客里看,我这里就记录下自己的思路,由于本人能力有限,会有很多地方有更好的办法,仅供参考。
参考文章:https://blog.csdn.net/maosidiaoxian/article/details/41673425

jsoup的简介

使用项目原话:jsoup是一个Java库来处理实际的HTML。它提供了一个非常方便的API来提取和操纵数据,使用最好的DOM,CSS和jquery-like方法

项目地址:https://github.com/jhy/jsoup
中文文档:http://www.open-open.com/jsoup/

jsoup的配置

jsoup的配置很简单,需要在gradle中添加以下依赖

compile 'org.jsoup:jsoup:1.10.2'

由于jsoup需要获取网络数据,所以记得添加网络权限

<uses-permission android:name="android.permission.INTERNET" />

正文

已经通过Jsoup.connect(URL).get();方法获取到整个网站的代码,但是发现我需要的新闻板块是这样的,没有任何标识

<a target="_blank" href="/special/detail_375109.html">台湾液晶面板厂中华映管申请破产重整!  负债348亿新台币</a>

于是我采用获取全文有href的a标签,同时为了排除网站上一些板块的导航入口,href中又必须含有"/connect/“和”/html/"

new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                        Document doc = Jsoup.connect(WebMainURL).get();
                        Elements els = doc.select("a[href]");
                        for(Element el : els){
                            String href=el.attr("href");
                            if (href.contains("/special/") && href.contains("html"))
                            {
                                Log.e("完整",el.toString());
                                Log.e("链接",href);
                                Log.e("内容",el.text());
                                Log.e("分隔","\n"+"----------------------------------------------------------");
                            }
                    }
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();

结果如下
在这里插入图片描述

然后就可以进去获取更具体的作者、时间、内容等信息,为了方便测试,我这里先指定进入一个网址。并找到了它代码中的作者和时间

<div class="public-time">
					<span>文章来源: 半导体投资联盟  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 发布时间:2018-12-14</span>
				</div>

谢天谢地它终于有class了!感人!,马上读取出它的text,
发现是这样的 文章来源: 半导体投资联盟 发布时间:2018-12-14
获取到“发布时间”的位置,用2个substring得到作者和时间

 if(href.equals("/special/detail_375109.html")){
      Document docDetailRaw = Jsoup.connect(WebMainURL+href).get();
      Elements els_docDetail = docDetailRaw.select("div.public-time");
      String doc_detail = els_docDetail.text();
      int timeInString = els_docDetail.text().indexOf("发布时间");
      String doc_detailAuthor = doc_detail.substring(5,timeInString).trim();
      String doc_detailTime = doc_detail.substring(timeInString+5).trim();
      Log.e("author",doc_detailAuthor);
      Log.e("time",doc_detailTime);

}

log如下

12-15 06:36:30.622 9210-9226/com.example.asus.spidertest E/author: 半导体投资联盟      
12-15 06:36:30.622 9210-9226/com.example.asus.spidertest E/time: 2018-12-14

同理,文章的内容也能轻而易举爬出来了

    Elements els_docContent = docDetailRaw.select("div.main-text");
    String doc_content = els_docContent.text();
    Log.e("content",doc_content);

但是发现没有图片
参考(Android开发技巧——TextView加载HTML的图片及代码显示问题)

https://blog.csdn.net/maosidiaoxian/article/details/41673425

最后成功!在这里插入图片描述

后面就是一些封装成Bean,连接数据库等操作,可以自己根据需求来。

最后附上源码
https://download.csdn.net/download/qq_39830579/10853980

猜你喜欢

转载自blog.csdn.net/qq_39830579/article/details/85013813