java抓取页面内容,代码和详解步骤

 在做项目或要利用其他网站内容进行数据收集或分析时,想为我所用,通常需要分三步:

1、将目标页面进行爬取,获取原内容;

2、根据爬取的内容,进行分析规律,然后进行分解提取,获取想要的数据内容和格式;

3、基于内容和格式,做后续的分析和事情


1、Java代码-----将目标页面进行爬取,获取原内容;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import java.io.IOException;

public class getContentbyUrl{

    public static void main(String[] args) {
    //1.创建httpclient(相当于打开一个浏览器)
    CloseableHttpClient httpClient = HttpClients.createDefault();
    

    //2.创建get请求(相当于浏览器输入网址)
    HttpGet request = new HttpGet("www.baidu.com");

    CloseableHttpResponse response = null;
    try {
        //3.执行get请求(相当于输入网址后敲回车键)
        response = httpClient.execute(request);

        //4.判断响应状态是否为200
        if(response.getStatusLine().getStatusCode() == org.apache.http.HttpStatus.SC_OK){
            
            //5.获取响应内容即页面内容
            org.apache.http.HttpEntity httpEntity = response.getEntity();
            String html = org.apache.http.util.EntityUtils.toString(httpEntity, "utf-8");

            //打印出来
            System.out.println(html);
        } else {
            //如果返回状态不是200,比如404(页面不存在)等
            System.out.println("返回状态不是200");                
            System.out.println(EntityUtils.toString(response.getEntity(), "utf-8"));
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        //6.关闭
        HttpClientUtils.closeQuietly(response);
        HttpClientUtils.closeQuietly(httpClient);
    }
}
}

2、根据上面获得的内容,进行分解提取;

这一步,需要根据目标页面和获取内容不同,通过正则表达式去分解提取页面里自己想要的元素。

//对html,进行截取,从 “data":[ 之后开始
String gpString = html.substring(html.indexOf("\"data\":[")+8,html.indexOf("],\"error\":null}"));

//以 },为分割获取对应的数组数据
String[] gpStrings= gpString.split("},");

java.util.List<orgs> list = new java.util.ArrayList();

for(int i=0;i< gpStrings.length;i++){
    String temp = gpStrings[i].substring(1).replace("{","");
    String[] temps = temp.split("\",");
    orgs ors = new orgs();
    if(temps.length > 2){                                                
       ors.setGpCode(temps[1].substring(temps[1].indexOf(":\"")+2));                    
       ors.setGpName(temps[2].substring(temps[2].indexOf(":\"")+2));
    }

    list.add(ors);
}

3、基于内容和格式,做后续的分析和事情

 这一步就要根据自己要做的事情而定了

 

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

产技一体

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值