本来想用 import org.jsoup.Jsoup;
或者 import org.htmlparser.Parser;
可是 android 不支持 sun.com 原生java字节码执行 ,而是自成一家.
只能靠自己写了 ,例如: beanshell 抓取CSDN极客头条内容
TestHtmlParser.bsh
import java.net.*;
import java.io.*;
import org.json.*;
String utf8(String str){
return new String(str.getBytes("8859_1"),"UTF-8");
}
String getText(String txt,char c, tag){
int begin = txt.indexOf(c);
if (begin > -1){
begin += 1;
end = txt.indexOf(tag,begin);
if (begin<end){
return txt.substring(begin,end);
} else {
return null;
}
} else {
return null;
}
}
getPage(String url){
String prefix = "<a href=";
String buf;
String line;
try {
Url = new URL(url);
conn = Url.openConnection();
ins = new DataInputStream(conn.getInputStream());
while((line= ins.readLine()) != null){
buf = line.trim();
if (buf.startsWith(prefix)){
String href = getText(buf,‘"‘,‘"‘);
//print(href);
if (href != null && href.startsWith("http://")){
if (! href.startsWith("http://www.csdn.net")){
print(href);
print(utf8(getText(buf,‘>‘,"</a>")));
}
}
}
}
ins.close();
} catch(e) {
print(e);
}
}
getPage("http://geek.csdn.net/new");
bsh for android : HTML parser,布布扣,bubuko.com
原文地址:http://blog.csdn.net/belldeep/article/details/25499913