在互联网发达的今天,程序员往往开发的过程中需要一些稳定的网站数据.这个时候往往有些接口数据会收费,为了方便开发.程序员会使用爬虫技术抓取数据.爬虫往往分几种:网页UR.L抓取,
根据接口抓取等等.下面介绍是根据URL抓取相应数据
1 //抓取森林防火最新页面的URL
2 public void getSlhz(){
3 String strURL="http://wwww.forestry.gov.cn/Common/index/3563.html";
4 URL url;
5
6 try{
7 url = new URL(strURL);
8 HttpURLConnection httpConn=(HttpURLConnection)url.openConnection();
9 InputStreamReader input=new InputStreamReader(httpConn.getInputStream(),"utf-8");
10
11 BufferedReader buf= new BufferedReader(input);
12
13 String line="";
14 StringBuilder conf=new StringBuilder();
15 while((line=buf.readLine()))!=null){
16 conf.append(line);
17 }
18 String buf=conf.toString();
19 int beginIx=buf.indexOf("<ul> <li class=\"cl\"><a href=\"">);
20 int endIx=buf.indexOf("/" title=\"\"");
21 String result=buf.substring(beginIx,endIx);
22 String resl="http://www.forestry.gov.cn"+result.split("href=\"")[1];
23
24 System.out.println(resl);
25 }catch(Exception e){
26 e.printStackTrace();
27
28 }
29
30 }