半小时实现Java手撸网络爬虫框架!!,2024年最新JSP计算机毕设项目

return matcher.matches();

}

/**

  • 创建对应文件

  • @param content

  • @param urlPath

*/

public static void createFile(String content, String urlPath) {

/* 分割url */

String[] elems = divUrl(urlPath);

StringBuffer path = new StringBuffer();

File file = null;

for (int i = 1; i < elems.length; i++) {

if (i != elems.length - 1) {

path.append(elems[i]);

path.append(File.separator);

file = new File(“D:” + File.separator + path.toString());

}

if (i == elems.length - 1) {

Pattern pattern = Pattern.compile(“\w+\.[a-zA-Z]+”);

Matcher matcher = pattern.matcher(elems[i]);

if ((matcher.matches())) {

if (!file.exists()) {

file.mkdirs();

}

String[] fileName = elems[i].split(“\.”);

file = new File(“D:” + File.separator + path.toString()

  • File.separator + fileName[0] + “.txt”);

try {

file.createNewFile();

writer = new BufferedWriter(new OutputStreamWriter(

new FileOutputStream(file)));

writer.write(content);

writer.flush();

writer.close();

System.out.println(“创建文件成功”);

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

/**

  • 获取页面的超链接并将其转换为正式的A标签

  • @param href

  • @return

*/

public static String getHrefOfInOut(String href) {

/* 内外部链接最终转化为完整的链接格式 */

String resultHref = null;

/* 判断是否为外部链接 */

if (href.startsWith(“http://”)) {

resultHref = href;

} else {

/* 如果是内部链接,则补充完整的链接地址,其他的格式忽略不处理,如:a href=“#” */

if (href.startsWith(“/”)) {

resultHref = “http://www.oschina.net” + href;

}

}

return resultHref;

}

/**

  • 截取网页网页源文件的目标内容

  • @param content

  • @return

*/

public static String getGoalContent(String content) {

int sign = content.indexOf(“<pre class=”");

String signContent = content.substring(sign);

int start = signContent.indexOf(“>”);

int end = signContent.indexOf(“”);

return signContent.substring(start + 1, end);

}

/**

  • 检查网页源文件中是否有目标文件

  • @param content

  • @return

*/

public static int isHasGoalContent(String content) {

return content.indexOf(“<pre class=”");

}

}

HrefOfPage.java 此类为获取页面的超链接

package com.sreach.spider;

/**

  • @author binghe

*/

public class HrefOfPage {

/**

  • 获得页面源代码中超链接

*/

public static void getHrefOfContent(String content) {

System.out.println(“开始”);

String[] contents = content.split(“<a href=”");

for (int i = 1; i < contents.length; i++) {

int endHref = contents[i].indexOf(“”");

String aHref = FunctionUtils.getHrefOfInOut(contents[i].substring(

0, endHref));

if (aHref != null) {

String href = FunctionUtils.getHrefOfInOut(aHref);

if (!UrlQueue.isContains(href)

&& href.indexOf(“/code/explore”) != -1

&& !VisitedUrlQueue.isContains(href)) {

UrlQueue.addElem(href);

}

}

}

System.out.println(UrlQueue.size() + “–抓取到的连接数”);

System.out.println(VisitedUrlQueue.size() + “–已处理的页面数”);

}

}

UrlDataHanding.java 此类主要是从未访问队列中获取url,下载页面,分析url,保存已访问url等操作,实现Runnable接口

package com.sreach.spider;

/**

  • @author binghe

*/

public class UrlDataHanding implements Runnable {

/**

  • 下载对应页面并分析出页面对应的URL放在未访问队列中。

  • @param url

*/

public void dataHanding(String url) {

HrefOfPage.getHrefOfContent(DownloadPage.getContentFormUrl(url));

}

public void run() {

while (!UrlQueue.isEmpty()) {

dataHanding(UrlQueue.outElem());

}

}

}

UrlQueue.java 此类主要是用来存放未访问的URL队列

package com.sreach.spider;

import java.util.LinkedList;

/**

  • @author binghe

*/

public class UrlQueue {

/** 超链接队列 */

public static LinkedList urlQueue = new LinkedList();

/** 队列中对应最多的超链接数量 */

public static final int MAX_SIZE = 10000;

public synchronized static void addElem(String url) {

urlQueue.add(url);

}

public synchronized static String outElem() {

return urlQueue.removeFirst();

}

public synchronized static boolean isEmpty() {

return urlQueue.isEmpty();

}

public static int size() {

return urlQueue.size();

}

public static boolean isContains(String url) {

return urlQueue.contains(url);

}

}

VisitedUrlQueue.java 主要是保存已访问过的URL,使用HashSet来保存,主要是考虑到每个访问过的URL是不同。HashSet刚好符合这个要求

package com.sreach.spider;

import java.util.HashSet;

/**

  • 已访问url队列

  • @author binghe

*/

public class VisitedUrlQueue {

public static HashSet visitedUrlQueue = new HashSet();

public synchronized static void addElem(String url) {

visitedUrlQueue.add(url);

}

public synchronized static boolean isContains(String url) {

return visitedUrlQueue.contains(url);

}

public synchronized static int size() {

return visitedUrlQueue.size();

}

}

Test.java 此类为测试类

import java.sql.SQLException;

import com.sreach.spider.UrlDataHanding;

import com.sreach.spider.UrlQueue;

/**

  • @author binghe

*/

public class Test {

public static void main(String[] args) throws SQLException {

String url = “http://www.oschina.net/code/explore/achartengine/client/AndroidManifest.xml”;

String url1 = “http://www.oschina.net/code/explore”;

String url2 = “http://www.oschina.net/code/explore/achartengine”;

String url3 = “http://www.oschina.net/code/explore/achartengine/client”;

UrlQueue.addElem(url);

UrlQueue.addElem(url1);

UrlQueue.addElem(url2);

UrlQueue.addElem(url3);

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

深知大多数同学面临毕业设计项目选题时,很多人都会感到无从下手,尤其是对于计算机专业的学生来说,选择一个合适的题目尤为重要。因为毕业设计不仅是我们在大学四年学习的一个总结,更是展示自己能力的重要机会。

因此收集整理了一份《2024年计算机毕业设计项目大全》,初衷也很简单,就是希望能够帮助提高效率,同时减轻大家的负担。
img
img
img

既有Java、Web、PHP、也有C、小程序、Python等项目供你选择,真正体系化!

由于项目比较多,这里只是将部分目录截图出来,每个节点里面都包含素材文档、项目源码、讲解视频

如果你觉得这些内容对你有帮助,可以添加VX:vip1024c (备注项目大全获取)
img

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

深知大多数同学面临毕业设计项目选题时,很多人都会感到无从下手,尤其是对于计算机专业的学生来说,选择一个合适的题目尤为重要。因为毕业设计不仅是我们在大学四年学习的一个总结,更是展示自己能力的重要机会。

因此收集整理了一份《2024年计算机毕业设计项目大全》,初衷也很简单,就是希望能够帮助提高效率,同时减轻大家的负担。
[外链图片转存中…(img-U9TMzLtm-1712556987887)]
[外链图片转存中…(img-nMqUe0cl-1712556987888)]
[外链图片转存中…(img-g3xUXJQv-1712556987888)]

既有Java、Web、PHP、也有C、小程序、Python等项目供你选择,真正体系化!

由于项目比较多,这里只是将部分目录截图出来,每个节点里面都包含素材文档、项目源码、讲解视频

如果你觉得这些内容对你有帮助,可以添加VX:vip1024c (备注项目大全获取)
[外链图片转存中…(img-etFf8PXV-1712556987888)]

  • 12
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值