网络爬虫
网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本。另外一些不常使用的名字还有蚂蚁、自动索引、模拟程序或者蠕虫。
使用String类提供的方法制作简单的网络爬虫
实现步骤:
1、分析网页找到内容页面链接
2、通过Java程序获取网页源码
3、利用String类提供的方法对网页源码进行字符串处理
String类的用法:http://blog.youkuaiyun.com/gfd54gd5f46/article/details/54704182
分析网页
- 这里就找一个ip查询的页面进行测试
- 会返回一个IP地址及运营商
使用谷歌浏览器分析一下这个网页
F12 开启调试窗口
- 进入Network
刷新网络之后截取到网页数据
然后开始分析网页返回的内容
- 点击http://www.ip138.com/ 之后发现里面的内容很多,并没有找到我们需要的IP
查看ic.asp页面 ,找到了需要的内容页面
拷贝网页的链接和页面编码
url: http://1212.ip138.com/ic.asp
编码:GB2312
1、获取页面源码
打开Eclipse创建一个Java工程
/**
* 通过网络链接获取HTML源码
* @param pageUrl
* @param encoding
* @return content
*/
public static String getHtmlFromUrl(String pageUrl,String encoding){
//用来存放HTML源码
String content = "";
try {
//初始化
URL url = new URL(pageUrl);
//通过url打开一个连接
URLConnection conn = url.openConnection();
//设置请求属性,伪装成用户,否则会被检查出来是爬虫
conn.setRequestProperty("User-Agent",
"Mozilla/31.0 (compatible; MSIE 10.0; Windows NT; DigExt)");
//拿到一个输入流
InputStream is = conn.getInputStream();
//初始化一个扫描仪,将输入流跟编码传递进去
Scanner scanner = new Scanner(is,encoding);
//遍历扫描仪
while(scanner.hasNextLine()){
//得到一行数据
String str = scanner.nextLine();
//将每一行的数据给到content
content += str + "\n";
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//将内容返回
return content;
}
这样就拿到了网页的源码了。
2、截取IP地址
有了网页的源码之后,我们就可以使用String类相应的方法对源码内容进行截取,首先分析一下源码的内容
我们需要获取的ip地址是在 中括号[] 里面的:67.198.247.221
这时候需要用到String类里面的方法:
int indexOf(int ch) //返回指定字符在此字符串中第一次出现处的索引。
String substring(int beginIndex, int endIndex) //返回一个新字符串,它是此字符串的一个子字符串。
- String getIpFromContent(String content)
/**
* 通过源码内容获取IP地址
* @param content
* @return ip
*/
public static String getIpFromContent(String content){
//找到以'[' 开始 出现的位置
int beginIndex = content.indexOf("[");
//找到以']' 结束 出现的位置
int endIndex = content.indexOf("]");
//截取从'[' 到 ']' 里面的内容
//因为substring()方法 是 [beginIndex,endIndex)
//第一个参数是开区间,第二个参数是闭区间,所以第一个参数beginIndex 需要加1
String ip = content.substring(beginIndex + 1, endIndex);
//返回截取的IP地址
return ip;
}
这样就获取到了IP地址
3、截取运营商名称
借鉴前面获取IP地址的步骤
- 我们要截取 ‘美国’ 这个字段
- String getOperatorFromContent(String content)
/**
* 从源码内容中获取运营商
* @param content
* @return operator
*/
public static String getOperatorFromContent(String content){
//找到以 '来' 开始 出现的位置
int beginIndex = content.indexOf("来自:");
//找到以 '</center>' 结束 出现的位置
int endIndex = content.indexOf("</center>");
//截取'来' 到 '</center>' 中间的内容
//因为我们只需要截取运营商的名字,所以beginIndex需要加3
String operator = content.substring(beginIndex + 3, endIndex);
//返回运营商名称
return operator;
}
到这里我们的简单爬虫就实现了。
拿到页面的源码之后就可以使用String类里的方法进行字符串处理了。
完整源码
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
/**
* 2017-1-25
* @author LingDu
* 功能:简单的网络爬虫,获取IP地址以及运营商名称
*/
public class StringDemo {
/**
* 通过网络链接获取HTML源码
* @param pageUrl
* @param encoding
* @return content
*/
public static String getHtmlFromUrl(String pageUrl,String encoding){
//用来存放HTML源码
String content = "";
try {
//初始化
URL url = new URL(pageUrl);
//通过url打开一个连接
URLConnection conn = url.openConnection();
//设置请求属性,伪装成用户,否则会被检查出来是爬虫
conn.setRequestProperty("User-Agent",
"Mozilla/31.0 (compatible; MSIE 10.0; Windows NT; DigExt)");
//拿到一个输入流
InputStream is = conn.getInputStream();
//初始化一个扫描仪,将输入流跟编码传递进去
Scanner scanner = new Scanner(is,encoding);
//遍历扫描仪
while(scanner.hasNextLine()){
//得到一行数据
String str = scanner.nextLine();
//将每一行的数据给到content
content += str + "\n";
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//将内容返回
return content;
}
/**
* 通过源码内容获取IP地址
* @param content
* @return ip
*/
public static String getIpFromContent(String content){
//找到以'[' 开始 出现的位置
int beginIndex = content.indexOf("[");
//找到以']' 结束 出现的位置
int endIndex = content.indexOf("]");
//截取从'[' 到 ']' 里面的内容
//因为substring()方法 是 [beginIndex,endIndex)
//第一个参数是开区间,第二个参数是闭区间,所有beginIndex + 1
String ip = content.substring(beginIndex + 1, endIndex);
//返回截取的IP地址
return ip;
}
/**
* 从源码内容中获取运营商
* @param content
* @return operator
*/
public static String getOperatorFromContent(String content){
//找到以 '来' 开始 出现的位置
int beginIndex = content.indexOf("来自:");
//找到以 '</center>' 结束 出现的位置
int endIndex = content.indexOf("</center>");
//截取'来' 到 '</center>' 中间的内容
//因为我们只需要截取运营商的名字,所以beginIndex需要加3
String operator = content.substring(beginIndex + 3, endIndex);
//返回运营商名称
return operator;
}
public static void main(String[] args) {
String content = getHtmlFromUrl("http://1212.ip138.com/ic.asp", "GB2312");
System.out.println(getIpFromContent(content));
System.out.println(getOperatorFromContent(content));
}
}