|
上面的是python,给你个java版的参考吧
- import java.io.*;
- import java.util.*;
- import org.apache.http.*;
- import org.apache.http.impl.client.*;
- import org.apache.poi.hssf.usermodel.*;
- import com.google.gson.Gson;
- import com.google.gson.reflect.TypeToken;
- import org.apache.http.HttpResponse;
- import org.apache.http.util.EntityUtils;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.impl.client.HttpClients;
- import org.apache.http.client.ClientProtocolException;
- public class Test {
-
- public static String getHtml(String url) throws ClientProtocolException, IOException{
-
- CloseableHttpClient http = HttpClients.createDefault();
- HttpGet get = new HttpGet(url);
- HttpResponse response = http.execute(get);
- HttpEntity entity = response.getEntity();
- String content = EntityUtils.toString(entity,"utf-8");
- return content;
-
- }
-
- public static List<Map<String,String>> json(String html){
-
- Gson gson = new Gson();
- List<Map<String,String>> list = gson.fromJson(html, new TypeToken<List<Map<String,String>>>() {}.getType());
- return list;
-
- }
-
- public static void writeExcel(List<Map<String,String>> list,String file) throws IOException{
-
- int y = 0;
- HSSFWorkbook excelbook = new HSSFWorkbook();
- HSSFSheet sheet = excelbook.createSheet("sheet1");
- FileOutputStream fot = new FileOutputStream(file);
- for(int x = 0;x < list.size();x++){
- HSSFRow row =sheet.createRow(x);
- for(String s : list.get(x).values()){
- row.createCell(y++).setCellValue(s);
- }
- y = 0;
- }
- excelbook.write(fot);
- fot.close();
- excelbook.close();
-
- }
-
- public static void main(String[] args) throws ClientProtocolException, IOException {
-
- String html = getHtml("http://data.eastmoney.com/DataCenter_V3/stock2016/jymx.ashx?pagesize=300&page=1&js=var%20CjnlcAlx¶m=&sortRule=-1&sortType=&gpfw=0&code=80479140&rt=25563331");
- html = html.split("data":")[1].split(","url")[0];
- List<Map<String,String>> list = json(html);
- writeExcel(list,"d:/excel.xls");
- }
- }
复制代码 |
评分
-
1
查看全部评分
-
|