抓取csdn上的各类别的文章 (制作csdn app 二)

this.id = id;

}

public String getDate()

{

return date;

}

public void setDate(String date)

{

this.date = date;

}

public String getImgLink()

{

return imgLink;

}

public void setImgLink(String imgLink)

{

this.imgLink = imgLink;

}

public String getContent()

{

return content;

}

public void setContent(String content)

{

this.content = content;

}

@Override

public String toString()

{

return “NewsItem [id=” + id + “, title=” + title + “, link=” + link + “, date=” + date + “, imgLink=” + imgLink

  • “, content=” + content + “, newsType=” + newsType + “]”;

}

}

CommonException.java

package com.zhy.bean;

public class CommonException extends Exception

{

public CommonException()

{

super();

// TODO Auto-generated constructor stub

}

public CommonException(String message, Throwable cause)

{

super(message, cause);

// TODO Auto-generated constructor stub

}

public CommonException(String message)

{

super(message);

// TODO Auto-generated constructor stub

}

public CommonException(Throwable cause)

{

super(cause);

// TODO Auto-generated constructor stub

}

}

Constaint.java

package com.zhy.csdn;

public interface Constaint

{

public static final int NEWS_TYPE_YEJIE = 1;

public static final int NEWS_TYPE_YIDONG = 2;

public static final int NEWS_TYPE_YANFA = 3;

public static final int NEWS_TYPE_CHENGXUYUAN = 4;

public static final int NEWS_TYPE_YUNJISUAN = 5;

}

DataUtil.java

package com.zhy.csdn;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.URL;

import com.zhy.bean.CommonException;

public class DataUtil

{

/**

  • 返回该链接地址的html数据

  • @param urlStr

  • @return

  • @throws CommonException

*/

public static String doGet(String urlStr) throws CommonException

{

StringBuffer sb = new StringBuffer();

try

{

URL url = new URL(urlStr);

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setRequestMethod(“GET”);

conn.setConnectTimeout(5000);

conn.setDoInput(true);

conn.setDoOutput(true);

if (conn.getResponseCode() == 200)

{

InputStream is = conn.getInputStream();

int len = 0;

byte[] buf = new byte[1024];

while ((len = is.read(buf)) != -1)

{

sb.append(new String(buf, 0, len, “UTF-8”));

}

is.close();

} else

{

throw new CommonException(“访问网络失败!”);

}

} catch (Exception e)

{

throw new CommonException(“访问网络失败!”);

}

return sb.toString();

}

}

URLUtil.java

package com.zhy.csdn;

public class URLUtil

{

public static final String NEWS_LIST_URL = “http://www.csdn.net/headlines.html”;

public static final String NEWS_LIST_URL_YIDONG = “http://mobile.csdn.net/mobile”;

public static final String NEWS_LIST_URL_YANFA = “http://sd.csdn.net/sd”;

public static final String NEWS_LIST_URL_YUNJISUAN = “http://cloud.csdn.net/cloud”;

public static final String NEWS_LIST_URL_ZAZHI = “http://programmer.csdn.net/programmer”;

public static final String NEWS_LIST_URL_YEJIE = “http://news.csdn.net/news”;

/**

  • 根据文章类型,和当前页码生成url

  • @param newsType

  • @param currentPage

  • @return

*/

public static String generateUrl(int newsType, int currentPage)

{

currentPage = currentPage > 0 ? currentPage : 1;

String urlStr = “”;

switch (newsType)

{

case Constaint.NEWS_TYPE_YEJIE:

urlStr = NEWS_LIST_URL_YEJIE;

break;

case Constaint.NEWS_TYPE_YANFA:

urlStr = NEWS_LIST_URL_YANFA;

break;

case Constaint.NEWS_TYPE_CHENGXUYUAN:

urlStr = NEWS_LIST_URL_ZAZHI;

break;

case Constaint.NEWS_TYPE_YUNJISUAN:

urlStr = NEWS_LIST_URL_YUNJISUAN;

break;

default:

urlStr = NEWS_LIST_URL_YIDONG;

break;

}

urlStr += “/” + currentPage;

return urlStr;

}

}

NewsItemBiz.java业务类

package com.zhy.biz;

import java.util.ArrayList;

import java.util.List;

import org.jsoup.Jsoup;

import org.jsoup.nodes.Document;

import org.jsoup.nodes.Element;

import org.jsoup.select.Elements;

import com.zhy.bean.CommonException;

import com.zhy.bean.NewsItem;

import com.zhy.csdn.DataUtil;

import com.zhy.csdn.URLUtil;

/**

  • 处理NewItem的业务类

  • @author zhy

*/

public class NewsItemBiz

{

/**

  • 业界、移动、云计算

  • @param htmlStr

  • @return

  • @throws CommonException

*/

public List getNewsItems( int newsType , int currentPage) throws CommonException

{

String urlStr = URLUtil.generateUrl(newsType, currentPage);

String htmlStr = DataUtil.doGet(urlStr);

List newsItems = new ArrayList();

NewsItem newsItem = null;

Document doc = Jsoup.parse(htmlStr);

Elements units = doc.getElementsByClass(“unit”);

for (int i = 0; i < units.size(); i++)

{

newsItem = new NewsItem();

newsItem.setNewsType(newsType);

Element unit_ele = units.get(i);

Element h1_ele = unit_ele.getElementsByTag(“h1”).get(0);

Element h1_a_ele = h1_ele.child(0);

String title = h1_a_ele.text();

String href = h1_a_ele.attr(“href”);

newsItem.setLink(href);

newsItem.setTitle(title);

Element h4_ele = unit_ele.getElementsByTag(“h4”).get(0);

Element ago_ele = h4_ele.getElementsByClass(“ago”).get(0);

String date = ago_ele.text();

newsItem.setDate(date);

Element dl_ele = unit_ele.getElementsByTag(“dl”).get(0);// dl

Element dt_ele = dl_ele.child(0);// dt

try

{// 可能没有图片

Element img_ele = dt_ele.child(0);

String imgLink = img_ele.child(0).attr(“src”);

newsItem.setImgLink(imgLink);

} catch (IndexOutOfBoundsException e)

{

}

Element content_ele = dl_ele.child(1);// dd

String content = content_ele.text();

newsItem.setContent(content);

newsItems.add(newsItem);

}

return newsItems;

}

}

好了,最后就是测试了,这里使用单元测试,下面是测试代码和结果。

测试代码:

package com.zhy.test;

import java.util.List;

import com.zhy.bean.CommonException;

import com.zhy.bean.NewsItem;

import com.zhy.biz.NewsItemBiz;

import com.zhy.csdn.Constaint;

import com.zhy.csdn.DataUtil;

public class Test

{

@org.junit.Test

public void test01()

{

NewsItemBiz biz = new NewsItemBiz();

int currentPage = 1;

try

{

/**

  • 业界

*/

List newsItems = biz.getNewsItems(Constaint.NEWS_TYPE_YEJIE, currentPage);

for (NewsItem item : newsItems)

{

System.out.println(item);

}

System.out.println(“----------------------”);

/**

  • 程序员杂志

*/

newsItems = biz.getNewsItems(Constaint.NEWS_TYPE_CHENGXUYUAN, currentPage);

for (NewsItem item : newsItems)

{

System.out.println(item);

}

System.out.println(“----------------------”);

/**

  • 研发

*/

newsItems = biz.getNewsItems(Constaint.NEWS_TYPE_YANFA, currentPage);

for (NewsItem item : newsItems)

{

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

总结

笔者之前工作是在金融公司可能并不是特别追求技术,而笔者又是喜欢追求技术的人,所以格格不入,只能把目标放在互联网大厂了。也希望大家都去敢于尝试和追逐自己的梦想!
BATJ大厂Android高频面试题

觉得有收获的记得点赞,关注+收藏哦!你们的点赞就是我的动力!

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

图片转存中…(img-yng3WgeM-1713622043737)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

总结

笔者之前工作是在金融公司可能并不是特别追求技术,而笔者又是喜欢追求技术的人,所以格格不入,只能把目标放在互联网大厂了。也希望大家都去敢于尝试和追逐自己的梦想!
BATJ大厂Android高频面试题

[外链图片转存中…(img-xbh4qwtE-1713622043738)]

[外链图片转存中…(img-xA4xzcLp-1713622043739)]

[外链图片转存中…(img-WJAXr7Ws-1713622043740)]

[外链图片转存中…(img-FmcmTuP7-1713622043741)]

觉得有收获的记得点赞,关注+收藏哦!你们的点赞就是我的动力!

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 18
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值