app第二阶段冲刺第十天—— jsoup 2

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


 

今天写的是 jsoup  获取网页 html,抓取到的文章数据封装

 

实现代码如下:

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

import android.util.Log;
import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

//OkHttpUtils 获取网页 html
public class OkHttpUtils {

    final static String TAG = "OkHttpUtils";

    public static String OkGetArt(String url) {
        String html = null;
        OkHttpClient client = new OkHttpClient();//OkHttpClient  HTTP客户端
        Request request = new Request.Builder()
                .url(url)
                .build();
        try  {
            Response response = client.newCall(request).execute();
            //return
            html = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //Log.i(TAG, "OkGetArt: html "+html);
        return html;

    }
}
复制代码

 

复制代码
package com.example.crawler.Tools;
/***
 * 抓取到的文章数据封装
 */
//Article 为数据模型类,抓取那些数据类型
public class Article {

    private String title;
    private String author;
    private String imgUrl;
    private String context;
    private String articleUrl;


    //有几个属性还没用到,所以构造方法先用上这四个有爬取到数据的
    public Article(String title, String author, String imgUrl, String context, String articleUrl) {
        this.title = title;
        this.author = author;
        this.imgUrl = imgUrl;
        this.context = context;
        this.articleUrl = articleUrl;
    }


    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getImgUrl() {
        return imgUrl;
    }

    public void setImgUrl(String imgUrl) {
        this.imgUrl = imgUrl;
    }

    public String getContext() {
        return context;
    }

    public void setContext(String context) {
        this.context = context;
    }

    public String getArticleUrl() {
        return articleUrl;
    }

    public void setArticleUrl(String articleUrl) {
        this.articleUrl = articleUrl;
    }



    @Override
    public String toString() {
        return "Article{" +
                "title='" + title + '\'' +
                ", author='" + author + '\'' +
                ", imgUrl='" + imgUrl + '\'' +
                ", context='" + context + '\'' +
                ", articleUrl='" + articleUrl + '\'' +
                '}';
    }
}
复制代码

 

 

明天要写的是关于适配器相关的内容

 

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