前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >抓取html页面中的json数据

抓取html页面中的json数据

作者头像
Java架构师必看
发布2021-08-03 10:58:15
3.3K0
发布2021-08-03 10:58:15
举报
文章被收录于专栏:Java架构师必看Java架构师必看

抓取html页面中的json数据

强烈推介IDEA2020.2破解激活,IntelliJ IDEA 注册码,2020.2 IDEA 激活码

遇见问题:

在开发爬虫时,我们有时需要抓取页面中的ajax的json数据。

解决方案:

采用正则表达式,高端大气上档次,重点是简洁,举个栗子:

html页面:

代码语言:javascript
复制
上面省去N行。。。。
var userLogin = function(){
		var jsonBean = {
    	number:"177***7495",
    	intLoginType:"4",
    	areaCode:"0471",
    	isBusinessCustType:"N",
    	identifyType:"B",
    	userLoginType:"4",
    	password:"",
    	randomPass:"",
    	noCheck:"N",
    	isSSOLogin:"Y",
    	sRand:"SSOLogin"
    	};
下面省去N行。。。。

正则抓取数据:

代码语言:javascript
复制
public static void praseStr() {
        String html = Models.readTxtFile("E:\\tmpTxt\\test0703.html");
        String any ="[\\s\\S]*" ;//任何东西
        StringBuffer regex = new StringBuffer("");
        regex.append("(number.*)").append(any);//目标字段,下同
        regex.append("(intLoginType.*)").append(any);
        regex.append("(areaCode.*)").append(any);
        regex.append("(isBusinessCustType.*)").append(any);
        regex.append("(identifyType.*)").append(any);
        regex.append("(userLoginType.*)").append(any);
        regex.append("(password.*)").append(any);
        regex.append("(randomPass.*)").append(any);
        regex.append("(noCheck.*)").append(any);
        regex.append("(isSSOLogin.*)").append(any);
        regex.append("(sRand.*)").append(any);
        Pattern p = Pattern.compile(regex.toString());
        Matcher m = p.matcher(html);
        int countAll = m.groupCount();
        StringBuffer json = new StringBuffer("{");
        if(m.find())
        for (int i=1;i<=countAll;i++){
            json.append(m.group(i)) ;
        }
        System.out.println(json.append("}").toString() );
       
    }

抓取结果:

{number:"177***7495",intLoginType:"4",areaCode:"0471",isBusinessCustType:"N",identifyType:"B",userLoginType:"4",password:"",randomPass:"",noCheck:"N",isSSOLogin:"Y",sRand:"SSOLogin"}

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档