- VisitedUrlQueue.java 已访问过的URL队列。
下面介绍一下每个类的源代码:
DownloadPage.java 此类要用到HttpClient组件。
package com.sreach.spider;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
/**
- @author binghe
*/
public class DownloadPage {
/**
-
根据URL抓取网页内容
-
@param url
-
@return
*/
public static String getContentFormUrl(String url) {
/* 实例化一个HttpClient客户端 */
HttpClient client = new DefaultHttpClient();
HttpGet getHttp = new HttpGet(url);
String content = null;
HttpResponse response;
try {
/* 获得信息载体 */
response = client.execute(getHttp);
HttpEntity entity = response.getEntity();
VisitedUrlQueue.addElem(url);
if (entity != null) {
/* 转化为文本信息 */
content = EntityUtils.toString(entity);
/* 判断是否符合下载网页源代码到本地的条件 */
if (FunctionUtils.isCreateFile(url)
&& FunctionUtils.isHasGoalContent(content) != -1) {
FunctionUtils.createFile(
FunctionUtils.getGoalContent(content), url);
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
client.getConnectionManager().shutdown();
}
return content;
}
}
FunctionUtils.java 此类的方法均为static方法
package com.sreach.spider;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
- @author binghe
*/
public class FunctionUtils {
/**
- 匹配超链接的正则表达式
*/
private static String pat = “http://www\.oschina\.net/code/explore/.*/\w+\.[a-zA-Z]+”;
private static Pattern pattern = Pattern.compile(pat);
private static BufferedWriter writer = null;
/**
- 爬虫搜索深度
*/
public static int depth = 0;
/**
-
以"/"来分割URL,获得超链接的元素
-
@param url
-
@return
*/
public static String[] divUrl(String url) {
return url.split(“/”);
}
/**
-
判断是否创建文件
-
@param url
-
@return
*/
public static boolean isCreateFile(String url) {
Matcher matcher = pattern.matcher(url);
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());
}
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Python工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Python开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以扫码获取!!!(备注Python)
开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**
[外链图片转存中…(img-vEvfkrx7-1713201908428)]
[外链图片转存中…(img-R7JkLSbb-1713201908429)]
[外链图片转存中…(img-osgWeyBp-1713201908429)]
[外链图片转存中…(img-mOuyRHaX-1713201908429)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以扫码获取!!!(备注Python)