2 fsyangrui fsyangrui 于 2014.11.20 13:50 提问

为什么我得到的文本没有内容,全都是逻辑代码,怎么样能获得网页里的文本

public class down {
String text="abc";
String URL="http://zhidao.baidu.com/question/1667030995892031787.html";
HttpClient client=new HttpClient();
GetMethod mythod=new GetMethod(URL);
public void get()
{
try{
int status=client.executeMethod(mythod);
if(status==HttpStatus.SC_OK)
{
text=mythod.getResponseBodyAsString();
System.out.print(text);
}
else{text="cba";}
}catch(Exception e){}

1个回答

u013762572
u013762572   2014.11.20 14:02
已采纳

因为我没找到你的HttpClient和GetMethod两个类,自己写了个方法,希望对你有用:
`public static void main(String[] args) throws Exception {
StringBuilder sb = new StringBuilder() ;
String url="http://zhidao.baidu.com/question/1667030995892031787.html";

    URL uri = new URL(url);
    HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
    conn.setDoInput(true);
    conn.setDoOutput(true);
    BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream() , "GBK"));
    String line = "" ;
    while((line = reader.readLine()) != null){
        sb.append(line+"\n");
    }
    reader.close();

    System.out.println(sb.toString());
}
    `

当你得到源码时,希望得到该想要的内容是,一般是要截取相应的字符串,,,,,,

Csdn user default icon
上传中...
上传图片
插入图片