抓取html页面中的json数据
强烈推介IDEA2020.2破解激活,IntelliJ IDEA 注册码,2020.2 IDEA 激活码
遇见问题:
在开发爬虫时,我们有时需要抓取页面中的ajax的json数据。
解决方案:
采用正则表达式,高端大气上档次,重点是简洁,举个栗子:
html页面:
上面省去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行。。。。
正则抓取数据:
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"}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有