提交 d6d86265 编写于 作者: 亦蔚然's avatar 亦蔚然

对代码进行重构

上级 583c9698
...@@ -82,4 +82,8 @@ ...@@ -82,4 +82,8 @@
- 算法 - 算法
- DFS 深度优先算法 - DFS 深度优先算法
- BFS 广度优先 - BFS 广度优先
- 重构
- 短方法:
- a.便于人脑理解
- b.越短越容易复用
- c.对于Java来说可以方便的对方法进行覆盖
...@@ -37,8 +37,32 @@ public class Main { ...@@ -37,8 +37,32 @@ public class Main {
continue; continue;
} }
// 判断是否是感兴趣滴内容【新浪站内的网页】 // 判断是否是感兴趣滴内容【新浪站内的网页】
// link.contains("sina.cn") && !link.contains("passport.sina.cn") && if (isInterestingLink(link)) {
if ((link.contains("news.sina.cn")) || "https://sina.cn".equals(link)) { Document doc = httpGetAndParseHtml(link);
// 使用CSS选择器,html中去获取
ArrayList<Element> links = doc.select("a");
links.stream().map(aTag -> aTag.attr("href")).forEach(linkPool::add);
// 假设这是一个新闻的详情页,就存入数据库,否则,就什么都不做
storeIntoDatabaseIfItIsNewsPage(doc);
processedLinks.add(link);
} else {
// 不感兴趣
continue;
}
}
}
/*
* 2、将表达不同逻辑的代码抽象为短方法
* 优点:
* a.便于人脑理解
* b.越短越容易复用
* c.对于Java来说可以方便的对方法进行覆盖
*/
// 通过http请求拿到HTML文档
private static Document httpGetAndParseHtml(String link) throws IOException {
try (CloseableHttpClient httpclient = HttpClients.createDefault()) { try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
if (link.startsWith("//")) { if (link.startsWith("//")) {
link = "https:" + link; link = "https:" + link;
...@@ -50,15 +74,13 @@ public class Main { ...@@ -50,15 +74,13 @@ public class Main {
System.out.println(link); System.out.println(link);
HttpEntity entity1 = response1.getEntity(); HttpEntity entity1 = response1.getEntity();
String html = EntityUtils.toString(entity1); String html = EntityUtils.toString(entity1);
return Jsoup.parse(html);
Document doc = Jsoup.parse(html);
// 使用CSS选择器,html中去获取
ArrayList<Element> links = doc.select("a");
for (Element aTag : links) {
// 获取href属性
linkPool.add(aTag.attr("href"));
} }
// 假设这是一个新闻的详情页,就存入数据库,否则,就什么都不做 }
}
// 若是新闻页面就存到数据库中
private static void storeIntoDatabaseIfItIsNewsPage(Document doc) {
ArrayList<Element> articleTags = doc.select("article"); ArrayList<Element> articleTags = doc.select("article");
if (!articleTags.isEmpty()) { if (!articleTags.isEmpty()) {
for (Element articleTag : articleTags) { for (Element articleTag : articleTags) {
...@@ -66,21 +88,29 @@ public class Main { ...@@ -66,21 +88,29 @@ public class Main {
System.out.println(titile); System.out.println(titile);
} }
} }
processedLinks.add(link);
} }
/*
* 1、将长的判断条件抽取为不同的方法
*/
// 感兴趣的链接
private static boolean isInterestingLink(String link) {
return (isNewsPage(link) || isIndexPage(link) && isNotLoginPage(link));
} }
} else {
// 不感兴趣 // 首页
continue; private static boolean isIndexPage(String link) {
return "https://sina.cn".equals(link);
} }
// 新闻页
private static boolean isNewsPage(String link) {
return link.contains("news.sina.cn");
} }
// try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
// HttpGet httpGet = new HttpGet("https://sina.cn/"); // 登录页
// try (CloseableHttpResponse response1 = httpclient.execute(httpGet)) { private static boolean isNotLoginPage(String link) {
// System.out.println(response1.getStatusLine()); return !link.contains("passport.sina.cn");
// HttpEntity entity1 = response1.getEntity();
// System.out.println(EntityUtils.toString(entity1));
// }
// }
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册