获取单个页面的所有信息

来源:互联网 发布:淘宝推广计划怎么写 编辑:程序博客网 时间:2024/06/14 10:56
package cn.edu.hfut.dmic.contentextractor;

import java.io.File;
import java.io.IOException;

import javax.lang.model.util.Elements;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import jxl.*;
import jxl.write.*;

public class addToExcel {

    public static void main(String[] args) throws IOException {
        try  
        {  
        //生成excel
        WritableWorkbook book=Workbook.createWorkbook(new File("c:/wexcel/noah-fund.xls"));  
        //生成名为“第一页”的工作表,参数0表示这是第一页  
        WritableSheet sheet=book.createSheet("第一页",0);  
        Label label1=new Label(0,0,"代码");  
        sheet.addCell(label1);
        Label label2=new Label(1,0,"名称");  
        sheet.addCell(label2);
        Label label3=new Label(2,0,"单位净值");  
        sheet.addCell(label3);
        Label label4=new Label(3,0,"累计净值");  
        sheet.addCell(label4);
        Label label5=new Label(4,0,"单位净值");  
        sheet.addCell(label5);
        Label label6=new Label(5,0,"累计净值");  
        sheet.addCell(label6);
        Label label7=new Label(6,0,"日增长值");  
        sheet.addCell(label7);
        Label label8=new Label(7,0,"日增长率");  
        sheet.addCell(label8);
        Label label9=new Label(8,0,"基金类型");  
        sheet.addCell(label9);
        Label label10=new Label(9,0,"购买状态");  
        sheet.addCell(label10);
        Label label120=new Label(10,0,"赎回状态");  
        sheet.addCell(label120);
        Label label11=new Label(11,0,"基金经理");  
        sheet.addCell(label11);
        Label label12=new Label(12,0,"成立时间");  
        sheet.addCell(label12);
        Label label13=new Label(13,0,"基金状态");  
        sheet.addCell(label13);
/*——————————————————————————————————————————获取基金数据——————————————————————————————————————————*/        
        Document doc = Jsoup.connect("http://www.noah-fund.com/html/fundtools/initDaQuan.html").get();
//        String script1=doc.select("script").get(15).data();
//        System.out.println("script1:"+script1);
/*统计指定元素的个数*/    
                int num = doc.select("td[bgcolor=#FFFFFF]").size();
                System.out.println("num:"+num);
/*统计当前页面基金个数*/
                int fundnum=doc.select("a[href*=/fundtools/FundInfoAction.jsp?symbol=]").size();
                System.out.println("fundnum:"+fundnum);
/*直接获取每一行的基金数据数据*/
                for(int i=0;i<num;i++){
                String tags=doc.select("td[bgcolor=#FFFFFF]").get(i).text();
                System.out.print(tags+" ");
                if((i+1)%11==0)
                    System.out.println();
                }
/*如何获取所有基金的数据?*/
//                Element pagehref=doc.select("a[href*=javascript:jumpPage]").last();
//                String  pagelink=pagehref.attr("abs:href");
//                System.out.println("最后一页:"+pagelink);
/*获取所有基金的绝对路径*/
                for(int i=0;i<fundnum;i++){
                    Element links=doc.select("a[href*=/fundtools/FundInfoAction.jsp?symbol=]").get(i);
                    String fundString=links.attr("abs:href");
//                    System.out.println(fundString);     
                    
/*获取子页面基金主页数据*/                
                    Document document =Jsoup.connect(fundString).get();
                    Element ifr=document.select("iframe").first();
                    String linkString=ifr.attr("abs:src");
//                System.out.println(linkString);
/*获取基金份额+此展规模*/
//                    String fundpercent=document.select("div.pflo-info-box").first().select("p").text();
//                    System.out.println(fundpercent);
                   
/*获取单个基金的详细信息*/
                    Document fundDocument=Jsoup.connect(linkString).get();
                   
                    String fundtype=fundDocument.select("td[width=160]").first().text().substring(5);
                    Label label14=new Label(8,i+1,fundtype);  
                    sheet.addCell(label14);
                    String isbuy=fundDocument.select("td[width=160]").get(1).text().substring(3, 5);
                    Label label15=new Label(9,i+1,isbuy);  
                    sheet.addCell(label15);
                    String issell=fundDocument.select("td[width=160]").get(1).text().substring(9, 11);
                    Label label19=new Label(10,i+1,issell);  
                    sheet.addCell(label19);
                    String manager=fundDocument.select("tr[class=row1]").get(1).children().get(1).text().substring(5);
                    Label label16=new Label(11,i+1,manager);  
                    sheet.addCell(label16);
                    String setdate=fundDocument.select("tr[class=row1]").get(1).children().get(2).text().substring(5);
                    Label label17=new Label(12,i+1,setdate);  
                    sheet.addCell(label17);
                    String status=fundDocument.select("div.btn-box").text();
                    Label label18=new Label(13,i+1,status);  
                    sheet.addCell(label18);
                   
                    System.out.println(fundtype+" "+isbuy+" "+manager+" "+setdate+" "+status);
                }
/*基金详细信息:代码~累计净值*/
                for(int i=0;i<num;i++){
                    int x=i/11+1;
                    int y=(i-1)%11;
                if((i+1)%11==2){
                    String fundcode=doc.select("td[bgcolor=#FFFFFF]").get(i).text();
                    Label label=new Label(y,x,fundcode);  
                    sheet.addCell(label);
                    System.out.print("fundcode:"+fundcode);
                }
                if((i+1)%11==3){
                    String fundname=doc.select("td[bgcolor=#FFFFFF]").get(i).text();
                    Label label=new Label(y,x,fundname);  
                    sheet.addCell(label);
                    System.out.print("fundname:"+fundname);
                }                
                if((i+1)%11==4){
                    String price=doc.select("td[bgcolor=#FFFFFF]").get(i).text();
                    Label label=new Label(y,x,price);  
                    sheet.addCell(label);
                    System.out.print("price:"+price);
                }
                if((i+1)%11==5){
                    String pricesum=doc.select("td[bgcolor=#FFFFFF]").get(i).text();
                    Label label=new Label(y,x,pricesum);  
                    sheet.addCell(label);
                    System.out.print("  pricesum:"+pricesum);
                }
                if((i+1)%11==6){
                    String price1=doc.select("td[bgcolor=#FFFFFF]").get(i).text();
                    Label label=new Label(y,x,price1);  
                    sheet.addCell(label);
                    System.out.print("  price1:"+price1);
                }
                if((i+1)%11==7){
                    String pricesum1=doc.select("td[bgcolor=#FFFFFF]").get(i).text();
                    Label label=new Label(y,x,pricesum1);  
                    sheet.addCell(label);
                    System.out.print("  pricesum1:"+pricesum1);
                }
                if((i+1)%11==8){
                    String dayplus=doc.select("td[bgcolor=#FFFFFF]").get(i).text();
                    Label label=new Label(y,x,dayplus);  
                    sheet.addCell(label);
                    System.out.print("  dayplus:"+dayplus);
                }
                if((i+1)%11==9){
                    String dayrate=doc.select("td[bgcolor=#FFFFFF]").get(i).text();
                    Label label=new Label(y,x,dayrate);  
                    sheet.addCell(label);
                    System.out.println("  dayrate:"+dayrate);
                }
            }
                book.write();  
                book.close();  
                System.out.println("成功将数据写入excel");
                }catch(Exception e)  
                {  
                System.out.println(e);  
                }         

    }

}

0 0
原创粉丝点击