close

 

Chrome開啓https://www.tdcc.com.tw/smWeb/QryStock.jsp ,按F12 開啟開發者模式點選Network

 

先輸入個證券代號 2330 並按下查詢,可以觀察到Network底下一些傳輸的訊息

 

首先,最下面的方塊可以看到Request URL Form Data

 

 

Form dataview source code可以看到需要傳的參數

 

 

有了這兩個資訊以後就可以擷取股權分散表

 

我是用apachehttpclient所以需要下面的Lib

 

 

首先先寫個基本款HTTPGet的輸入參數為 request url + “?” + form data

 

CloseableHttpClient httpclient = HttpClients.createDefault();

HttpGet httpGet = new HttpGet( "https://www.tdcc.com.tw/smWeb/QryStock.jsp?SCA_DATE=20161125&SqlMethod=StockNo&StockNo=2330&StockName=&sub=%ACd%B8%DF" );

CloseableHttpResponse response1 = httpclient .execute( httpGet );

        

try {

System. out .println( response1 .getStatusLine());

HttpEntity entity1 = response1 .getEntity();

BufferedReader br = new BufferedReader( new InputStreamReader( entity1 .getContent()));

String line ;

StringBuffer response = new StringBuffer();

while (( line = br .readLine()) != null ) {

response .append( line + "\n" );

     }

System. out .println( response.toString() );

     br .close();

EntityUtils.consume( entity1 );

   } finally {

         response1 .close();

     }

但會遇到這樣問題

 

把上面的Code修改一下HttpClient建立的問題,即可避開這個問題

SSLContext sslContext = null ;

     try {

         sslContext = new SSLContextBuilder().loadTrustMaterial( null , new TrustStrategy() {

@Override

public boolean isTrusted( final X509Certificate[] arg0 , final String arg1 )

              throws CertificateException {

 

              return true ;

            }

         }).build();

         } catch (Exception e ) {

         throw new HttpException( "can not create http client." , e );

         }

           

SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory( sslContext ,

                  new NoopHostnameVerifier());

         CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory( sslSocketFactory ).build();

 

就可以抓到資料了

arrow
arrow
    創作者介紹
    創作者 KC 的頭像
    KC

    Programmer in Taiwan

    KC 發表在 痞客邦 留言(0) 人氣()