app第二阶段冲刺第九天—— jsoup 1

作者:@kuaiquxie
作者的github:https://github.com/bitebita
本文为作者原创,如需转载,请注明出处:https://www.cnblogs.com/dzwj/p/16260582.html


 

 

今天开始写爬虫了,从网上的资料可以知道,爬虫可以用  jsoup   来写,还可以用  python  来写,今天试试水,先用  jsoup  ,现在开搞。

 

我们还是先将布局文件先写好

复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <TextView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <TextView
        android:id="@+id/author"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <TextView
        android:id="@+id/context"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <TextView
        android:id="@+id/articleUrl"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>
复制代码

 

复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/lv_mytest"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>
复制代码

 

接下来我们写什么呢,先用Java写爬取数据吧,

由于放在之前那个项目里面,害怕将项目弄的无法运行,所以单独建立一个新项目,将爬虫实现在这个上面。

复制代码
package com.example.crawler.Tools;

import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;

//GetData 解析 html 整理成列表Article 模型数据
public class GetData {

    private static final String TAG ="GetData" ;

    /**
     * 抓取精选文章
     * @param html
     * @return  ArrayList<Article> articles
     */
    public static ArrayList<Article> spiderArticle(String html){
        ArrayList<Article> articles = new ArrayList<>();

        Document document = (Document) Jsoup.parse(html);
        Elements elements = document
                .select("ul[class=feed-list-hits feed-list-index]")
                .select("li[class=feed-row-wide J_feed_za feed-haojia]");

        Log.i(TAG, "spiderArticle: elements " +elements.html());

        for (Element element : elements) {
            String title = element
                    .select("h5[class=feed-block-title has-price]")
                    .text();

            String author = element
                    .select("div[class=z-feed-foot]")
                    .select("span[class=feed-block-extras]")
                    .select("a")
                    .select("span")
                    .text();

            String imgurl = element
                    .select("div[class=z-feed-img]")
                    .select("a")
                    .select("img")
                    .attr("src");

            String context = element
                    .select("div[class=feed-block-descripe]")
                    .text();

            String articleUrl = element
                    .select("div[class=z-feed-img ]")
                    .select("a")
                    .attr("href");

            Article article = new Article(title,author,imgurl,context,articleUrl);
            articles.add(article);
            //Log.e("DATA>>",article.toString());
        }
        return articles;
    }
}
复制代码

 

明天接着写获取网页 html,对抓取到的文章数据封装

 

posted @   kuaiquxie  阅读(25)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示