import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.URL;
import java.nio.CharBuffer;
import java.util.HashMap;
import org.cyberneko.html.parsers.DOMParser;
import org.xml.sax.InputSource;
public class Crawler {
public static void main(String[] args) {
String url = "http://www.sina.com.cn";
getLinksByNeko(getPage(url, "gbk"));
}
public static String getPage(String url, String encoding){
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(
new URL(url).openStream(),encoding));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
CharBuffer bos = CharBuffer.allocate(20480);
StringBuilder sb = new StringBuilder();
try {
while (in.read(bos) != -1) {
bos.flip();
sb.append(bos.toString());
}
} catch (IOException e1) {
e1.printStackTrace();
}finally{
if(in != null)
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
private static void getLinksByNeko(String Page) {
DOMParser parser = new DOMParser();
HashMap<String,String> map = new HashMap<String,String>();
try {
parser.setFeature("http://xml.org/sax/features/namespaces", false);
StringReader sin = new StringReader(Page);
parser.parse(new InputSource(sin));
org.w3c.dom.Document doc = parser.getDocument();
org.w3c.dom.NodeList products = org.apache.xpath.XPathAPI
.selectNodeList(doc, "//A");
org.w3c.dom.Node node = null;
for (int i = 0; i < products.getLength(); i++) {
node = products.item(i);
map.put(node.getAttributes().getNamedItem("href").getNodeValue(),node.getTextContent());
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(map.toString());
System.out.println(map.size());
}
}
分享到:
相关推荐
开源项目-neko-neko-godi.zip,DI容器
赠送jar包:neko-htmlunit-2.27.jar; 赠送原API文档:neko-htmlunit-2.27-javadoc.jar; 赠送源代码:neko-htmlunit-2.27-sources.jar; 赠送Maven依赖信息文件:neko-htmlunit-2.27.pom; 包含翻译后的API文档:...
赠送jar包:neko-htmlunit-2.33.jar; 赠送原API文档:neko-htmlunit-2.33-javadoc.jar; 赠送源代码:neko-htmlunit-2.33-sources.jar; 赠送Maven依赖信息文件:neko-htmlunit-2.33.pom; 包含翻译后的API文档:...
Learning and using Neko is very easy. You can easily extend the language with C libraries. The Virtual Machine is very lightweight and well optimized so it's fast! The VM can be easily embeded into ...
windows下实现的linux的neko
Professional haXe and Neko
java运行依赖jar包
赠送jar包:neko-htmlunit-2.33.jar; 赠送原API文档:neko-htmlunit-2.33-javadoc.jar; 赠送源代码:neko-htmlunit-2.33-sources.jar; 赠送Maven依赖信息文件:neko-htmlunit-2.33.pom; 包含翻译后的API文档:...
赠送jar包:neko-htmlunit-2.27.jar; 赠送原API文档:neko-htmlunit-2.27-javadoc.jar; 赠送源代码:neko-htmlunit-2.27-sources.jar; 赠送Maven依赖信息文件:neko-htmlunit-2.27.pom; 包含翻译后的API文档:...
docs for the neko rpg 2
docs for the neko rpg game
docs for the neko rpg 3
Neko虚拟机见快照构建可以在每个的摘要部分的“工件”链接中找到已编译的二进制文件。 对于macOS,可以在一个命令中使用构建最新的master分支的Neko快照: brew install neko --HEAD 。 它将安装所需的依赖项,构建...
Neko RPGXP for android 是在由韩国友人开发的一款在谷歌市场上免费下载的RPGXP软件 仅供测试
np21w PC-9800系列仿真器Neko项目21 / W 基于PC-98模拟器Neko Project II / 21,我们将发布经过修改的Neko Project 21 / W,以承受PC-98后期模型的特殊硬件仿真和复制。 为后期PC-98添加了功能,例如Super MPU,...
Neko服务器
Neko Lovers主页
用于Neko ProjectII 0.82的反汇编动态跟踪插件(Beta0.8版本)。请自行下载Neko ProjectII 0.82版本并用此补丁覆盖原文件(请备份原文件!)插件在本人开发机上使用VS2003编译完成。详细操作请参阅ZIP包内文档。 Bug ...
该项目基于昵称为sawada的人发布的资源,该人使用了Neko Project II的代码。 欢迎提供帮助和建议。
neko