獲取單個頁面的所有資訊
阿新 • • 發佈:2019-02-02
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);
}
}
}
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);
}
}
}