博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java网页数据抓取
阅读量:4128 次
发布时间:2019-05-25

本文共 5727 字,大约阅读时间需要 19 分钟。

对于加密的网站还没去研究,不知道能不能抓取,现在只是对一些没有加密的网站进行网页数据抓取。刚刚开始写的时候以为很多网站都能抓取,但是发现很多都加密了,本来以为一些地址可以通过网页数据检测工具测出他的数据变化,但是只能监测到一些通过js显示的数据,依然不能抓取到加密的网站。嗨,这个问题以后再说吧。

我抓取的网站是手机号查询和身份证查询的网站。这个是查询身份证的网站,源地址是这个,但当你输入自己的身份证是地址就会变成根据这个变化就可以抓取到输入特定身份证的网页源代码,再通过解析读取到需要的数据。下面是代码:

import java.net.* ;import java.io.* ;import java.util.regex.* ;public class Capture{	public static void main(String args[])throws Exception{		System.out.println("*************************手机号查询************************") ;		System.out.println("我的位置是:" + new GrabMobile().grabMobileLocation("15023141745")) ;		System.out.println("手机卡类型是:" + new GrabMobile().grabMobileType("15023141745")) ;		System.out.println("我的邮编是:" + new GrabMobile().grabMobilePost("15023141745")) ;		System.out.println("*************************身份证查询************************") ;		System.out.println("我的性别是:" + new GrabIdentity().grabIdentitySex("362203199208243575")) ;		System.out.println("我的生日是:" + new GrabIdentity().grabIdentityBirth("362203199208243575")) ;		System.out.println("我的家乡是:" + new GrabIdentity().grabIdentityHome("362203199208243575")) ;	}}class GrabMobile{	public String grabMobileLocation(String m)throws Exception{		String strUrl = "http://www.ip138.com:8080/search.asp?action=mobile&mobile=" + m;		URL url = new URL(strUrl) ;		HttpURLConnection httpUrlCon = (HttpURLConnection)url.openConnection() ;		InputStreamReader inRead = new InputStreamReader(httpUrlCon.getInputStream(),"GBK") ;		BufferedReader bufRead = new BufferedReader(inRead) ;		StringBuffer strBuf = new StringBuffer() ;		String line = "" ;		while ((line = bufRead.readLine()) != null) {			strBuf.append(line);		}		String strStart = "卡号归属地" ;		String strEnd = "卡 类 型";		String strAll = strBuf.toString() ;				int start = strAll.indexOf(strStart) ;				int end = strAll.indexOf(strEnd) ;				String result = strAll.substring(start+42,end-33) ;		result = drawChMob(result) ;		return result ;	}	public String grabMobileType(String m)throws Exception{		String strUrl = "http://www.ip138.com:8080/search.asp?action=mobile&mobile=" + m;		URL url = new URL(strUrl) ;		HttpURLConnection httpUrlCon = (HttpURLConnection)url.openConnection() ;		InputStreamReader inRead = new InputStreamReader(httpUrlCon.getInputStream(),"GBK") ;		BufferedReader bufRead = new BufferedReader(inRead) ;		StringBuffer strBuf = new StringBuffer() ;		String line = "" ;		while ((line = bufRead.readLine()) != null) {			strBuf.append(line);		}		String strStart = "卡 类 型" ;		String strEnd = "区 号";		String strAll = strBuf.toString() ;				int start = strAll.indexOf(strStart) ;				int end = strAll.indexOf(strEnd) ;				String result = strAll.substring(start+12,end) ;		result = drawChMob(result) ;		result = result.substring(1) ;		return result ;	}	public String grabMobilePost(String m)throws Exception{		String strUrl = "http://www.ip138.com:8080/search.asp?action=mobile&mobile=" + m;		URL url = new URL(strUrl) ;		HttpURLConnection httpUrlCon = (HttpURLConnection)url.openConnection() ;		InputStreamReader inRead = new InputStreamReader(httpUrlCon.getInputStream(),"GBK") ;		BufferedReader bufRead = new BufferedReader(inRead) ;		StringBuffer strBuf = new StringBuffer() ;		String line = "" ;		while ((line = bufRead.readLine()) != null) {			strBuf.append(line);		}		String strStart = "邮 编" ;		String strEnd = "更详细的..";		String strAll = strBuf.toString() ;				int start = strAll.indexOf(strStart) ;				int end = strAll.indexOf(strEnd) ;				String result = strAll.substring(start+40,end-55) ;		return result ;	}	public String drawChMob(String str){		StringBuffer strBuf = new StringBuffer() ;		String regex="([\u4e00-\u9fa5]+)";		Matcher matcher = Pattern.compile(regex).matcher(str);		while(matcher.find()){			strBuf.append(matcher.group(0)).toString() ;		}		return strBuf.toString() ;	}}class GrabIdentity{	public String grabIdentitySex(String userid)throws Exception{		String strUrl = "http://qq.ip138.com/idsearch/index.asp?action=idcard&userid=" + userid + "&B1=%B2%E9+%D1%AF";		URL url = new URL(strUrl) ;		HttpURLConnection httpUrlCon = (HttpURLConnection)url.openConnection() ;		InputStreamReader inRead = new InputStreamReader(httpUrlCon.getInputStream(),"GBK") ;		BufferedReader bufRead = new BufferedReader(inRead) ;		StringBuffer strBuf = new StringBuffer() ;		String line = "" ;		while ((line = bufRead.readLine()) != null) {			strBuf.append(line);		}		String strStart = " 别" ;		String strEnd = "出生日期";		String strAll = strBuf.toString() ;				int start = strAll.indexOf(strStart) ;				int end = strAll.indexOf(strEnd) ;				String result = strAll.substring(start+7,end) ;		result = drawCh(result) ;		return result ;	}	public String grabIdentityBirth(String userid)throws Exception{		String strUrl = "http://qq.ip138.com/idsearch/index.asp?action=idcard&userid=" + userid + "&B1=%B2%E9+%D1%AF";		URL url = new URL(strUrl) ;		HttpURLConnection httpUrlCon = (HttpURLConnection)url.openConnection() ;		InputStreamReader inRead = new InputStreamReader(httpUrlCon.getInputStream(),"GBK") ;		BufferedReader bufRead = new BufferedReader(inRead) ;		StringBuffer strBuf = new StringBuffer() ;		String line = "" ;		while ((line = bufRead.readLine()) != null) {			strBuf.append(line);		}		String strStart = "出生日期:" ;		String strEnd = "" ;		String strEnd = "
部分或" ; String strAll = strBuf.toString() ; int start = strAll.indexOf(strStart) ; int end = strAll.indexOf(strEnd) ; String result = strAll.substring(start+31,end) ; return result ; } public String drawCh(String str){ StringBuffer strBuf = new StringBuffer() ; String regex="([\u4e00-\u9fa5]+)"; Matcher matcher = Pattern.compile(regex).matcher(str); if(matcher.find()){ str = strBuf.append(matcher.group(0)).toString() ; } return str ; }}

待会传上改装成的android小程序,可以手机号查询和身份证查询。

转载地址:http://qqwvi.baihongyu.com/

你可能感兴趣的文章
(python版)《剑指Offer》JZ01:二维数组中的查找
查看>>
Spring MVC中使用Thymeleaf模板引擎
查看>>
PHP 7 的五大新特性
查看>>
深入了解php底层机制
查看>>
PHP中的stdClass 【转】
查看>>
XHProf-php轻量级的性能分析工具
查看>>
OpenCV gpu模块样例注释:video_reader.cpp
查看>>
就在昨天,全球 42 亿 IPv4 地址宣告耗尽!
查看>>
如果你还不了解 RTC,那我强烈建议你看看这个!
查看>>
Mysql复制表以及复制数据库
查看>>
Linux下SVN客户端使用教程
查看>>
Linux分区方案
查看>>
如何使用 systemd 中的定时器
查看>>
git命令速查表
查看>>
linux进程监控和自动重启的简单实现
查看>>
OpenFeign学习(三):OpenFeign配置生成代理对象
查看>>
OpenFeign学习(四):OpenFeign的方法同步请求执行
查看>>
OpenFeign学习(五):OpenFeign请求结果处理及重试控制
查看>>
OpenFeign学习(六):OpenFign进行表单提交参数或传输文件
查看>>
Ribbon 学习(二):Spring Cloud Ribbon 加载配置原理
查看>>