分析
1.打開網址 http://gs.amac.org.cn/amac-infodisc/res/pof/fund/index.html
2.用fiddler抓取數據:
url:
http://gs.amac.org.cn/amac-infodisc/api/pof/fund?rand=0.8173862954864839&page=0&size=20
headers:
POST http://gs.amac.org.cn/amac-infodisc/api/pof/fund?rand=0.8173862954864839&page=0&size=20 HTTP/1.1
Host: gs.amac.org.cn
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Referer: http://gs.amac.org.cn/amac-infodisc/res/pof/fund/index.html
Content-Type: application/json
X-Requested-With: XMLHttpRequest
Content-Length: 2
Connection: keep-alive
HTTP/1.1 200
Server: nginx
Date: Mon, 18 Feb 2019 05:50:31 GMT
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
3.先用postman嘗試
總是報錯
4.postman修改錯誤
參考網址https://ask.csdn.net/questions/699862?sort=votes_count
得出可能出錯的地方:"POST請求這個地址,POST發送的參數內容爲{},要不會報錯,發送{}就可以了"
重新嘗試,成功:
5.編寫代碼
package com.hlhlo.law.tyc.service.impl;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hlhlo.downdata.entity.Fund;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.IOException;
import java.util.List;
public class ATest {
public static void main(String[] args) {
//獲取請求連接
Connection con = Jsoup.connect("http://gs.amac.org.cn/amac-infodisc/api/pof/fund");
//請求頭設置
con.header("Host", "gs.amac.org.cn");
con.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0");
con.header("Accept-Encoding", "gzip, deflate");
con.header("Accept", "application/json, text/javascript, */*; q=0.01");
con.header("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
con.header("Referer", "http://gs.amac.org.cn/amac-infodisc/res/pof/fund/index.html");
con.header("Content-Type", "application/json");
con.header("X-Requested-With", "XMLHttpRequest");
con.header("Content-Length", "2");
con.header("Connection", "keep-alive");
con.data("page", String.valueOf(1));//第幾頁
con.data("size", String.valueOf(100));//一頁顯示幾行
con.data("rand", "0.45045581928993783");
con.requestBody("{}");//POST發送的參數內容爲{},要不會報錯,發送{}就可以了
try {
Document doc = con.ignoreContentType(true).post();
String body = doc.select("body").text();
ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readTree(body);
JsonNode totalPagesNode = rootNode.path("totalPages");
String totalPagestr = mapper.writeValueAsString(totalPagesNode);
int totalPages = Integer.parseInt(totalPagestr);
System.out.println("totalPages="+totalPages);
JsonNode recordsNode = rootNode.path("content");//內容
String recodesJson = mapper.writeValueAsString(recordsNode);
JavaType recordType = mapper.getTypeFactory().constructParametricType(List.class, Fund.class);
List<Fund> list = mapper.readValue(recodesJson, recordType);//得到具體的內容
if (list != null) {
for (Fund fund : list) {
System.out.println(fund.getFundName());
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}