昆明理工大学 搜索引擎大报告

数据抓取子系统设计

班级:

学号:

姓名:

一、 源程序

package com.liu.Echo;

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL;

import java.nio.charset.Charset; import java.util.Map; import java.util.Vector;

class HttpRequester {

/**

1

/** * 发送GET请求 * * @param urlString * URL地址 * @return 响应对象 * @throws IOException */ public HttpRespons sendGet(String urlString) throws IOException { } return this.send(urlString, "GET", null, null); } public HttpRequester() { this.defaultContentEncoding = Charset.defaultCharset().name(); //默认的内容字符编private String defaultContentEncoding;

* 发送GET请求 * * @param urlString * URL地址 * @param params * 参数集合 * @return 响应对象 * @throws IOException */ public HttpRespons sendGet(String urlString, Map<String, String> params) } /** * 发送GET请求 * * @param urlString * URL地址 * @param params * 参数集合 * @param propertys * 请求属性 * @return 响应对象 * @throws IOException */ public HttpRespons sendGet(String urlString, Map<String, String> params, } /** * 发送POST请求 * * @param urlString * URL地址 * @return 响应对象 * @throws IOException */ public HttpRespons sendPost(String urlString) throws IOException { } /**

2 return this.send(urlString, "POST", null, null); Map<String, String> propertys) throws IOException { return this.send(urlString, "GET", params, propertys); throws IOException { return this.send(urlString, "GET", params, null);

* 发送POST请求 * * @param urlString * URL地址 * @param params * 参数集合 * @return 响应对象 * @throws IOException */ public HttpRespons sendPost(String urlString, Map<String, String> params) } /** * 发送POST请求 * * @param urlString * URL地址 * @param params * 参数集合 * @param propertys * 请求属性 * @return 响应对象 * @throws IOException */ public HttpRespons sendPost(String urlString, Map<String, String> params, } /** * 发送HTTP请求 * * @param urlString * @return 响映对象 * @throws IOException */ private HttpRespons send(String urlString, String method, HttpURLConnection urlConnection = null; Map<String, String> parameters, Map<String, String> propertys) throws IOException { Map<String, String> propertys) throws IOException { return this.send(urlString, "POST", params, propertys); throws IOException { return this.send(urlString, "POST", params, null);

3

//判断请求方法的请求方式是否是get方式, if (method.equalsIgnoreCase("GET") && parameters != null) { } URL url = new URL(urlString); //创建URL对象,传入的地址为urlString urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod(method);//设置请求的方式 urlConnection.setDoOutput(true);//true可以接收来自urlConnection的输入 urlConnection.setDoInput(true);//true为可以发送urlConnection的输出 urlConnection.setUseCaches(false); //数据不可以从本地缓存中得到 StringBuffer param = new StringBuffer();//构建不带字符的字符缓冲区 int i = 0; //用于索引 for (String key : parameters.keySet()) {//使用迭代,得到prarameters的每一个 } urlString += param; //请求的地址 if (i == 0) param.append("?"); param.append("&"); else param.append(key).append("=").append(parameters.get(key)); //在缓冲区中构建成key=parameters.get(key)字符串 //parameters.get(key)是parameters里键为key对应的值 i++; key的视图 if (propertys != null) for (String key : propertys.keySet()) { } urlConnection.addRequestProperty(key, propertys.get(key));//设置请求属//请求方式为post if (method.equalsIgnoreCase("POST") && parameters != null) { }

4 StringBuffer param = new StringBuffer(); for (String key : parameters.keySet()) { } urlConnection.getOutputStream().write(param.toString().getBytes()); //获取此连接输出流 urlConnection.getOutputStream().flush();//刷新将写入的数据保存 urlConnection.getOutputStream().close(); //关闭输出流 param.append("&"); param.append(key).append("=").append(parameters.get(key));

httpResponser.defaultPort = urlConnection.getURL().getDefaultPort(); httpResponser.file = urlConnection.getURL().getFile(); httpResponser.host = urlConnection.getURL().getHost(); httpResponser.path = urlConnection.getURL().getPath(); httpResponser.port = urlConnection.getURL().getPort(); httpResponser.protocol = urlConnection.getURL().getProtocol(); httpResponser.query = urlConnection.getURL().getQuery();

5

httpResponser.urlString = urlString; //得到响应对象对应的各种值 String ecod = urlConnection.getContentEncoding(); //获得url的字符编码 if (ecod == null) ecod = this.defaultContentEncoding; /** * 得到响应对象 * * @param urlConnection * @return 响应对象 * @throws IOException */ private HttpRespons makeContent(String urlString, HttpURLConnection urlConnection) throws IOException { HttpRespons httpResponser = new HttpRespons(); try { InputStream in = urlConnection.getInputStream();//url的内容的获得字节流 BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(in,"UTF-8")); //创建字符缓冲流用于读取,字符 } return this.makeContent(urlString, urlConnection); 集为utf-8 httpResponser.contentCollection = new Vector<String>(); //创建线程安全集合 // 以下就是将读取到的流写入到集合中 StringBuffer temp = new StringBuffer(); String line = bufferedReader.readLine(); while (line != null) { } bufferedReader.close();//关闭流 httpResponser.contentCollection.add(line); temp.append(line).append("\r\n"); line = bufferedReader.readLine();

}

/** } /** } httpResponser.ref = urlConnection.getURL().getRef(); httpResponser.userInfo = urlConnection.getURL().getUserInfo(); httpResponser.content = new String(temp.toString().getBytes(), ecod); httpResponser.contentEncoding = ecod; httpResponser.code = urlConnection.getResponseCode(); httpResponser.message = urlConnection.getResponseMessage(); httpResponser.contentType = urlConnection.getContentType(); httpResponser.method = urlConnection.getRequestMethod(); httpResponser.connectTimeout = urlConnection.getConnectTimeout(); httpResponser.readTimeout = urlConnection.getReadTimeout(); return httpResponser; throw e; if (urlConnection != null) urlConnection.disconnect(); } catch (IOException e) { } finally { * 默认的响应字符集 */ public String getDefaultContentEncoding() { } /** * 设置默认的响应字符集 */ public void setDefaultContentEncoding(String defaultContentEncoding) { } this.defaultContentEncoding = defaultContentEncoding; return this.defaultContentEncoding;

* 响应对象

*/

class HttpRespons {

String urlString; //地址

6

int defaultPort; //默认端口 String file; //资源文件 String host; //主机名 String path; //路径 int port; //端口号 String protocol; //协议名称 String query; //查询部分 String ref; //引用 String userInfo; //用户信息 String contentEncoding; //内容编码 String content; //数据资源内容 String contentType; //内容类型 int code; //状态码 String message; //消息 String method; //方法 int connectTimeout; //超时时限 int readTimeout; //读取数据的超时时限 Vector<String> contentCollection; //存储内容 public String getContent() { } public String getContentType() { } public int getCode() { } public String getMessage() { } public Vector<String> getContentCollection() { } public String getContentEncoding() { } public String getMethod() {

7 return contentEncoding; return contentCollection; return message; return code; return contentType; return content;

} return method; public int getConnectTimeout() {

return connectTimeout;

}

public int getReadTimeout() { return readTimeout;

}

public String getUrlString() { return urlString;

}

public int getDefaultPort() { return defaultPort;

}

public String getFile() {

return file;

}

public String getHost() {

return host;

}

public String getPath() {

return path;

}

public int getPort() {

return port;

}

public String getProtocol() { return protocol;

}

public String getQuery() {

return query;

}

public String getRef() {

8

}

} return ref; public String getUserInfo() { } return userInfo;

public class test {

} public static void main(String[] args) { } try { } HttpRequester request = new HttpRequester(); HttpRespons hr = request.sendGet(""); System.out.println(hr.getUrlString()); System.out.println(hr.getProtocol()); System.out.println(hr.getHost()); System.out.println(hr.getPort()); System.out.println(hr.getContentEncoding()); System.out.println(hr.getMethod()); System.out.println(hr.getContent()); } catch (Exception e) { e.printStackTrace();

二、 程序分析

该程序是对指定网站上的数据进行抓取显示到我们的控制台上。需要将以输入流的方式获取到的网站的内容,并且将这些内容转换成字符流保存到文件中我们才能够从文件中读取获取的内容。

9

三、 程序改进

1、 改进的思路

1) 问题

我们通过发送请求从服务端获取到数据,获取到的内容里面包含着各种信息,我们可以对获取的数据内容进行处理分析,一个网页中包含着各种广告,各种链接等信息。如何内容中的超链接部分的链接地址从获取的数据中提取出来。

2) 改进

我们可以将内容中的超链接部分的链接地址从获取的数据中提取出来,在网页中超链接部分一般是用Html标签<a ></a>里,所以我们只用将<a></a>标签里面的数据提取出来放到集合里面就可以。

2、 改进部分代码

在HttpRequester类里面添加方法

public List<String> getLink(String Content){

List<String> list = new ArrayList<String>();

String regex=

"<a[^>]*href=(\"([^\"]*)\"|\'([^\']*)\'|([^\\s>]*))[^>]*>(.*?)</a>"; Pattern p = Pattern.compile(regex, Pattern.DOTALL);

Matcher m = pa.matcher(Content)

while (m.find())

{

list.add(m.group());

}

return list;

}

10

四、 总结

通过本次实验我学到很多,本次实验是老师给出一段代码,然后进行代码分析,首先是阅读代码了解该段代码所表达的含义。通过阅读代码知道该程序是对指定网站上的数据进行抓取显示到我们的控制台上。需要将以输入流的方式获取到的网站的内容,并且将这些内容转换成字符流保存到文件中我们才能够从文件中读取获取的内容。通过本次实验,让我将本学期的知识又进行了巩固,也是对这门课程的一个总结。通过读代码也让我将之前丢掉的java知识又重新温习了,也将需要掌握的很多java编程知识进行了巩固。使用java语言进行编程来完成搜索引擎课程中课程要求。

通过上搜索引擎这门课,我们根据老师上课所讲解的内容来完成老师所规定的实验内容。实验中,有很多不懂的地方也得到了老师和同学们的帮助,在此非常感谢老师和同学的悉心指导和帮助。当然我还有很多不懂的地方,但是我会努力的。

11

相关推荐