Jsoup+HttpClient擷取新浪新聞資料

來源:互聯網
上載者:User

Jsoup+HttpClient擷取新浪新聞資料

package com.test;import java.io.IOException;  import java.net.URI;  import org.apache.http.Header;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpGet;import org.apache.http.client.utils.URIBuilder; import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils;    /** * * 依賴 commons-httpclient-3.1.jar commons-codec-1.4.jar *  * @author tianjun * */public class PostTest {public static void main(String[] args) throws  Exception  {       // (1)構造HttpClient的執行個體    CloseableHttpClient httpCLient = HttpClients.createDefault();                      // 建立get請求執行個體          HttpGet httpget = new HttpGet() ;                //設定參數 //http://roll.news.sina.com.cn/s/channel.php?ch=01#col=91&spec=&type=&ch=01&k=&offset_page=0&offset_num=0&num=60&asc=&page=NaN //http://roll.news.sina.com.cn/s/channel.php?col=91&spec=&type=&ch=01&offset_page=0&offset_num=0&num=60&page=1                 URI uri = new URIBuilder()          .setScheme("http")        .setHost("roll.news.sina.com.cn")        .setPath("/s/channel.php")        .setParameter("ch", "01")        .setParameter("col", "91")         .setParameter("spec","")         .setParameter("type", "")         .setParameter("ch", "01")         .setParameter("offset_page", "0")         .setParameter("offset_num", "0")          .setParameter("num", "60")        .setParameter("page", "1")                         .build();                httpget.setURI(uri);      //佈建要求頭資訊         /* */                       httpget.setHeader("Accep", "*/*");        httpget.setHeader("Accept-Encoding","gzip, deflate");        httpget.setHeader("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");        httpget.setHeader("Connection","keep-alive");        httpget.setHeader("Host","roll.news.sina.com.cn");        httpget.setHeader("Referer","http://roll.news.sina.com.cn/s/channel.php?ch=01");        httpget.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0");        httpget.setHeader("Content-Type","text/html;charset=UTF-8");                          System.out.println("executing request "+httpget.getURI());                    try          {                            // 用戶端執行get請求 返迴響應實體              HttpResponse response = httpCLient.execute(httpget);                            // 伺服器響應狀態行              System.out.println(response.getStatusLine());                            Header[] heads = response.getAllHeaders();              // 列印所有回應標頭              for(Header h:heads){                  System.out.println(h.getName()+":"+h.getValue());              }                            // 擷取響應訊息實體              HttpEntity entity = response.getEntity();                            System.out.println("------------------------------------");                                                        if(entity != null){                                                    //響應內容                  System.out.println( new String(EntityUtils.toString(entity).getBytes("ISO-8859-1"),"gbk"));                                    System.out.println("----------------------------------------");                  // 響應內容長度                  System.out.println("響應內容長度:"+entity.getContentLength());              }                        } catch (ClientProtocolException e){              e.printStackTrace();          } catch (IOException e){              e.printStackTrace();          }finally{              httpCLient.getConnectionManager().shutdown();          }      }   }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.