用HttpClient和用HttpURLConnection做爬蟲發現爬取的程式碼少了的問題
阿新 • • 發佈:2018-11-30
最近在學習用java來做爬蟲但是發現不管用那種方式都是爬取的程式碼比網頁的原始碼少了很多
在網上查了很多都說是inputStream的緩衝區太小而爬取的網頁太大導致讀取出來的網頁程式碼不完整,但是後面發現並不是這個問
這個是用HttoClient所作的
public static String getHtml2(String url) { try { HttpGet httpRequest = new HttpGet(url); HttpClient httpclient = new DefaultHttpClient(); HttpResponse httpResponse= httpclient.execute(httpRequest); if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { InputStream input = httpResponse.getEntity().getContent(); byte[] b = new byte[1024]; int len = 0; StringBuffer buff= new StringBuffer(); while ((len = input.read(b)) != -1) { buff.append(new String(b)); } return buff.toString(); // 使用如下程式碼只返回40K // return EntityUtils.toString(httpResponse.getEntity(),"UTF-8");} }catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
這個使用HttpURLConnection做的
//使用HttpURLConnection獲取網頁內容 public static String getHtml(String url) {//獲取網頁內容 StringBuffer html=new StringBuffer(); if(!url.startsWith("http")) { url="https://"+url; } InputStreamReader inReader=null; BufferedReader bReader=null; HttpURLConnection htcon=null; try { URL u=new URL(url); //設定請求頭為獲取與原始碼一樣的程式碼 htcon=(HttpURLConnection)u.openConnection(); htcon.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"); htcon.setReadTimeout(2000);//設定讀取超時 htcon.setRequestMethod("POST");//設定請求方式 htcon.setConnectTimeout(2000);//設定連線超時 if(htcon.getResponseCode() == 200) {//如果頁面響應的話 inReader=new InputStreamReader(htcon.getInputStream(),"utf-8");//獲得頁面的輸入流 bReader=new BufferedReader(inReader); String line=""; while((line=bReader.readLine())!=null) { html.append(line); html.append("\n"); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { if(inReader!=null) { inReader.close(); } if(bReader!=null) { bReader.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return html.toString(); }
線上等解決方法,或等更新