java 爬取最近四川招标信息

2018-08-26 17:18:47来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

不废话直接上代码

public class Main {
static String urlString = "http://www.scggzy.gov.cn/Info/GetInfoListNew?keywords=&times=4&timesStart=&timesEnd=&province=&area=&businessType=&informationType=&industryType=&page=1&parm=1534929604640";
static String page="&page=";
@SuppressWarnings("static-access")
public static void main(String[] args)throws Exception{
// TODO Auto-generated method stub
open_url_test oUrl = new open_url_test();
if (oUrl.openurl(urlString)) {
readData rData = new readData();
JSONObject json = rData.readData(urlString);
JSONObject ob=JSONObject.fromObject(json);
String data= (String) ob.get("data");
data=data.substring(1,data.length()-1);
JSONArray json2=JSONArray.fromObject(data);
for (int i = 0; i < 10; i++) {
JSONObject jsonObject = (JSONObject) json2.get(i);
System.out.println("---------------------------");
System.out.println(jsonObject.get("Title"));
System.out.println(jsonObject.get("CreateDateStr"));
System.out.println(jsonObject.get("TableName"));
System.out.println(jsonObject.get("Link"));
System.out.println(jsonObject.get("username"));
System.out.println(jsonObject.get("province"));
System.out.println(jsonObject.get("businessType"));
System.out.println(jsonObject.get("NoticeType"));
}

}else{

}
}
}

-----------------------------------------------------------------------------------

public class open_url_test {
public static Logger logger = Logger.getLogger(open_url_test.class);
public boolean openurl(String url_infor) throws Exception{
URL url = new URL(url_infor);
// 连接类的父类,抽象类
URLConnection urlConnection = url.openConnection();
// http的连接类
HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
// 设定请求的方法,默认是GET(对于知识库的附件服务器必须是GET,如果是POST会返回405。流程附件迁移功能里面必须是POST,有所区分。)
httpURLConnection.setRequestMethod("GET");
// 设置字符编码
// httpURLConnection.setRequestProperty("Charset", "UTF-8");
// 打开到此 URL 引用的资源的通信链接(如果尚未建立这样的连接)。
int code = httpURLConnection.getResponseCode();
try {
InputStream inputStream = httpURLConnection.getInputStream();
System.out.println("连接成功");
logger.info("打开"+url_infor+"成功!");
return true;
}catch (Exception exception){
logger.info("打开"+url_infor+"失败!");
return false;
}
}
}

----------------------------------------------------------------------------------------------

public class readData {
public static JSONObject readData(String urlString) throws IOException, JSONException{
InputStream is = new URL(urlString).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
String jsonText = sb.toString();
JSONObject json = JSONObject.fromObject(jsonText);
return json;
} finally {
is.close();
}
}
}

----------------------------------------------------------

 结果:

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Java乐观锁实现之CAS操作

下一篇:3.4常用类(java学习笔记)Math和Random