import java.io.*;
import java.net.*;public class as{
public static void main(String[] args){
try{
URL a = new URL("http://www.baidu.com");
BufferedReader b1 = new BufferedReader(new InputStreamReader(a.openStream(), "gbk"));
//OutputStreamWriter b2 = new OutputStreamWriter( new FileOutputStream("e:/java/test/6/fdasfdas.txt",true));
String i = null;
while((i=b1.readLine())!=null){

//i=new String(i.getBytes("gbk"),"gbk");
 String s1 = i.substring(i.indexOf("搜索设置" ),i.indexOf("</a></p>"));
 System.out.println(s1);
 //System.out.println(s1);
//System.out.println(s1);
}
//b2.flush();
//b2.close();
b1.close();   }catch(MalformedURLException x){
System.out.println("地址错误");
}catch(IOException xx){
System.out.println("文件读写错误");
}
}
}
我想提取出网页源文件中“搜索设置”与“("</a></p>"))”的一段网址,但是老是不行,返回String index out of range: -1
请高手帮忙修正下代码

解决方案 »

  1.   

    使用Jsoup:
    Jsoup.parse(new URL("http://www.baidu.com"),10000).select("a:contains(登录)").get(0).attr("href")
      

  2.   

    结果就是 http://passport.baidu.com/?login&tpl=mn
      

  3.   

    你只能在搜索设置出现的行搜索才行,将下行:String s1 = i.substring(i.indexOf("搜索设置" ),i.indexOf("</a></p>"));修改为:String s1 = "";
    if (i.indexOf("搜索设置")>=0)
    s1 = i.substring(i.indexOf("搜索设置" ),i.indexOf("</a></p>"));