Java爬取网站ajax返回的数据

demo:爬取天津市网上办事大厅的数据

http://zwfw.tj.gov.cn/permissionitem_list_Sort2.jspx?ptype=P

开发时需要用到httpclient的jar包 https://download.csdn.net/download/qq_41032995/10998137

访问网址的时候抓包

Java爬取网站ajax返回的数据_第1张图片

查看一下需要提交的参数

Java爬取网站ajax返回的数据_第2张图片

3

上代码

public static String httpPostWithJSON(String url) throws Exception {
	 
	        HttpPost httpPost = new HttpPost(url);
	        CloseableHttpClient client = HttpClients.createDefault();
	        String respContent = null;
	        /*
	        json方式
	        JSONObject jsonParam = new JSONObject();  
	        jsonParam.put("pageNo", "1");
	        jsonParam.put("ptypekey", "p");
	        jsonParam.put("areaid", "120000");
	        jsonParam.put("sortkey", "");
	        jsonParam.put("deptidkey", "120000101");
	        jsonParam.put("xzxkkey", "");
	        jsonParam.put("selname", "");
	        StringEntity entity = new StringEntity(jsonParam.toString(),"utf-8");//解决中文乱码问题    
	        entity.setContentEncoding("UTF-8");    
	        entity.setContentType("application/json");    
	        httpPost.setEntity(entity);
	        System.out.println();
	        */
	        //表单方式
	        List pairList = new ArrayList(); 
	        /*
	        pairList.add(new BasicNameValuePair("pageNo", "2"));
	        pairList.add(new BasicNameValuePair("ptypekey", "p"));
	        pairList.add(new BasicNameValuePair("areaid", "120000"));
	        pairList.add(new BasicNameValuePair("sortkey", ""));
	        pairList.add(new BasicNameValuePair("deptidkey", "00012533X"));
	        pairList.add(new BasicNameValuePair("xzxkkey", ""));
	        pairList.add(new BasicNameValuePair("selname", ""));
	        */
	        httpPost.setEntity(new UrlEncodedFormEntity(pairList, "utf-8"));   
	        
	        HttpResponse resp = client.execute(httpPost);
	        HttpEntity he = resp.getEntity();
	        respContent = EntityUtils.toString(he,"UTF-8");
	        return respContent;
	    }


 public static void main(String[] args) throws Exception {
	    	String postParameter = "pageNo=&sortkey=&deptidkey=&xzxkkey=&ptypekey=P&selname=&areaid=120000";
	        String result = httpPostWithJSON("http://zwfw.tj.gov.cn/lawGuide/spsx_page_list.jspx?R="+Math.random()+"&"+postParameter);
	        System.out.println(result);
	    }

得到返回的数据后,你就可以根据需求进行下一步开发了。

你可能感兴趣的:(Java,httpclient,爬数据)