java爬虫--抓取携程酒店名称信息

public static String getChineseName(String id){
        String url ="https://hotels.ctrip.com/international/"+id+".html";
        String name = null;
        //创建一个默认的HttpClient  
        DefaultHttpClient httpclient = new DefaultHttpClient();  
        try {  
            HttpGet httpget = new HttpGet(url);  
            //打印请求地址  
            System.out.println("executing request " + httpget.getURI());  
            //创建响应处理器处理服务器响应内容  
            ResponseHandler responseHandler = new BasicResponseHandler();  
            //执行请求并获取结果  
            String responseBody = httpclient.execute(httpget, responseHandler);
            
            System.out.println("----------------------------------------");  
            //System.out.println(responseBody);  
            System.out.println("----------------------------------------");  
            
            Document doc = Jsoup.parse(responseBody);
            Elements content = doc.getElementsByClass("name");
            System.out.println(content.get(0).ownText());
            name = content.get(0).ownText();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {  
            //关闭连接管理器  
            httpclient.getConnectionManager().shutdown();  
        }  
        return name;
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章