分类:
2008-09-08 18:12:24
利用JAVA获取URL的内容,然后正则匹配结果,就完成了。
看代码:
//javac GoogleTranslator.java -encoding utf-8
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.regex.*;
public class GoogleTranslator{
public String getUrlContent(String path){
String rtn = "";
int c;
try{
java.net.URL l_url = new java.net.URL(path);
java.net.HttpURLConnection l_connection = (java.net.HttpURLConnection) l_url.openConnection();
l_connection.setRequestProperty("User-agent","Mozilla/4.0");
l_connection.connect();
InputStream l_urlStream = l_connection.getInputStream();
while (( ( c= l_urlStream.read() )!=-1)){
int all=l_urlStream.available();
byte[] b =new byte[all];
l_urlStream.read(b);
rtn+= new String(b, "UTF-8");
}
//Thread.sleep(2000);
l_urlStream.close();
}catch(Exception e){
e.printStackTrace();
}
return rtn;
}
public String GetText(String Src){
String Os=null;
try{
String pUrl=";
String pageContent =getUrlContent(pUrl);
if(!isNullOrEmpty(pageContent)){
Os= GetMatchString(pageContent,"(
然后保存为GoogleTranslator.java,然后使用:
javac GoogleTranslator.java -encoding utf-8
编译了,然后JAVA一下就可以