JAVA 爬蟲 獲取json格式的數據並生成javaBean


  一、寫在最前

    這是我第一篇非作業的博客,這一切都是因為該死的java爬蟲,我在爬取json數據是感受到了深深的惡意,而這惡意來源也非常清楚——python用起來簡單。

    所以我把問題解決了立馬想到要寫一篇帖子記錄下這個弄了一下午的問題。

    對了,同時提醒自己在最近把序列化,反射機制再過一遍,快忘完了!!!

  二、使用fastjson 解決

    這是目標請求:http://www.nmc.cn/f/rest/province

    他返回了json格式的數據,接下來獲取這些json格式的數據,放在一個實體類中

    

    1.首先是這個JavaBean的代碼 它只有一層 對於多層的這里有一篇博文明天看https://blog.csdn.net/a_rookie_xiaoming/article/details/52165502

package com.test;

public class Province {
    private String code;
    private String name;
    private String url;
    
    public Province(){}
    
    public Province(String code, String name, String url){
        this.code = code;
        this.name = name;
        this.url = url;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}

   2.然后先獲得這個請求返回的數據:

  

package com.test;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class getEntity {
    public static String get(){
        //創建客戶端
        CloseableHttpClient httpClient = HttpClients.createDefault();
        
        String entityr = "";
        //創建Get實例
        HttpGet httpGet = new HttpGet("http://www.nmc.cn/f/rest/province");
        
        //添加請求頭的信息,模擬瀏覽器訪問
        httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3573.0 Safari/537.36");

        try{
            //獲得Response
            CloseableHttpResponse response = httpClient.execute(httpGet);
            
            if(response.getStatusLine().getStatusCode() == 200){
                //當響應狀態碼為200時,獲得該網頁源碼並打印 
                String entity = EntityUtils.toString(response.getEntity(),"utf-8");
                entityr = entity;
            }
            
        }catch(Exception e){
            e.printStackTrace();
        }
        return entityr;
    }

}

    3.弄到實體類里面大功告成:

package com.test;

import java.util.List;
import com.alibaba.fastjson.JSON;


public class Demo {

    public static void main(String[] args) {
        //獲得響應的ajax,json格式的String
        String str = getEntity.get();
    
        //字符串序列化成集合
        List<Province> list= JSON.parseArray(str,Province.class);
        for(Province item: list){
            System.out.println(item.getCode());
            System.out.println(item.getName());
            System.out.println(item.getUrl());
        }    
    }
}

    結果:

    三、最后

    明天再看看有沒有更好更高效的解決方法


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM