利用HttpClient的工具類爬取直銷銀行的理財產品

http://blog.csdn.net/rocksteadypro/article/details/79087702

package app.spider;  
  
import java.io.IOException;  
import java.security.KeyManagementException;  
import java.security.KeyStoreException;  
import java.security.NoSuchAlgorithmException;  
import java.security.cert.CertificateException;  
import java.text.ParseException;  
import java.text.SimpleDateFormat;  
import java.util.ArrayList;  
import java.util.Date;  
import java.util.Iterator;  
import java.util.List;  
  
import org.apache.http.client.ClientProtocolException;  
import org.jsoup.Jsoup;  
import org.jsoup.nodes.Document;  
import org.jsoup.nodes.Element;  
import org.jsoup.select.Elements;  
  
import com.alibaba.fastjson.JSONObject;  
import app.db.model.Product;  
import app.utils.SpiderUtil;  
  
/**  
 * @author Rock  
 */  
public class QingHaiBank{  
    public static void main(String[] args) throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException, NumberFormatException, ParseException {  
        System.out.println(QingHaiBank.getData());  
        System.out.println(JSONObject.toJSON(QingHaiBank.getData()));  
    }  
      
    public static List<Product> getData() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException, NumberFormatException, ParseException{  
        String url = "https://www.dameidirectbank.com/pacctweb/InvestPrdShow.do";  
        
        String response = SpiderUtil.get(url, null, null, true, false);

       Document doc = Jsoup.parse(response);  
        //System.out.println(doc);  
        List<Element> eleList = new ArrayList<>();  
        Elements element1 = doc.getElementsByClass("licai");  
        Iterator<Element> ele1 = element1.iterator();  
        while(ele1.hasNext()) {  
            Element  e1 = ele1.next();  
            eleList.add(e1);  
        }  
        Elements element2 = doc.getElementsByClass("licai_r");  
        Iterator<Element> ele2 = element2.iterator();  
        while(ele2.hasNext()) {  
            Element  e2 = ele2.next();  
            eleList.add(e2);  
        }  
        List<Product> list = new ArrayList<>();  
        for (int i = 0; i < eleList.size(); i++) {  
            Element em = eleList.get(i);  
            list.add(getFormatData(em));  
        }  
        return list;  
    }  
      
    private static Product getFormatData(Element em) throws ClientProtocolException, IOException, NumberFormatException, ParseException {  
        Product product = new Product();  
        product.setBankName("青海銀行");  
        // InvPrdDetail('hjb18001T/2018年三江匯金寶第一期T款/50/20171228/20180103/20180223/停止交易/行內理財/1/5.6%');  
        String att = em.child(2).child(2).child(0).attr("onclick");  
        product.setPrdId(att.substring(14, att.split("/")[0].length()));  
        product.setPrdName(em.child(0).text());  
        product.setIncomeRate(em.child(1).child(0).text());  
        String day = em.child(2).child(0).child(1).text().replaceAll("天", "");  
        product.setInvestDuration(day);  
        product.setDurationUnit("天");  
        // 計算最少起投金額  
        // oneProductInfo.setFloor();  
        // 風險等級  
        product.setRiskLevel(em.child(2).child(1).child(1).text());  
        // 計算剩餘額度  
        // oneProductInfo.setLeftAmount();  
        product.setPrdType("理財");  
        product.setFinancingType("0");  
        // oneProductInfo.setIntervalAmount();  
        // 募集開始日募集結束日  
        product.setStartDay(att.split("/")[3]);  
        product.setEndDay(att.split("/")[4]);  
        // 生效日過期日  
        product.setActiveDay(getActiveDay(att.split("/")[5], day));  
        product.setOverdueDay(att.split("/")[5]);  
        return product;  
    }  
      
    private static String getActiveDay(String dates,String day) throws NumberFormatException, ParseException {  
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); // 日期格式  
        Date date = dateFormat.parse(dates); // 指定日期  
        Date newDate = addDate(date, Integer.valueOf(day)); // 指定日期加上20天  
        //System.out.println(dateFormat.format(date));// 輸出格式化後的日期  
        //System.out.println(dateFormat.format(newDate));  
        return dateFormat.format(newDate);  
    }  
    private static Date addDate(Date date, long day) throws ParseException {  
        long time = date.getTime(); // 得到指定日期的毫秒數  
        day = day * 24 * 60 * 60 * 1000; // 要加上的天數轉換成毫秒數  
        time -= day; // 相減得到新的毫秒數  
        return new Date(time); // 將毫秒數轉換成日期  
    }  
}  


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