java 爬取最近四川招标信息
不废话直接上代码
public class main {
static string urlstring = "http://www.scggzy.gov.cn/info/getinfolistnew?keywords=×=4×start=×end=&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();
}
}
}
----------------------------------------------------------
结果: