版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Caiaixiong/article/details/85230108
IP地址
用来标识网络中的一个通信实体的地址。通信实体可以是计算机、路由 器等。 比如互联网的每个服务器都要 有自己的IP地址,而每个局域网的计 算机要通信也要配置IP地址。路由器 是连接两个或多个网络的网络设备。
IP地址分类
- IPV4:32位地址,以点分十进制表示,如192.168.0.1
- IPV6:128位(16个字节)写成8个16位的无符号整数,每个整数用四个十六进制位表示,数之间用冒号(:)分开
特殊的IP
- 127.0.0.1 本机地址
- 192.168.0.0–192.168.255.255私有地址,属于非注册地址,专门为组织机构内部使用。
使用InetAddress
Java提供了InetAddress类代表IP地址,InetAddress下还有两个子类:Inet4Address、Inet6Address,他们分别代表IPv4地址和IPv6地址。
import java.net.InetAddress;
import java.net.UnknownHostException;
public class IPTest {
public static void main(String[] args) throws UnknownHostException {
//使用getLocalHost方法创建InetAddress对象 本机
InetAddress addr = InetAddress.getLocalHost();
System.out.println(addr.getHostAddress()); //返回:192.168.1.110
System.out.println(addr.getHostName()); //输出计算机名
//根据域名得到InetAddress对象
addr = InetAddress.getByName("www.baidu.com");
System.out.println(addr.getHostAddress()); //返回 baidu服务器的ip:
System.out.println(addr.getHostName()); //输出:www.baidu.com
//根据ip得到InetAddress对象
addr = InetAddress.getByName("123.56.138.176");
System.out.println(addr.getHostAddress()); //返回 ip:123.56.138.176
System.out.println(addr.getHostName()); //输出ip而不是域名:如果这个IP地 址不存在或DNS服务器不允许进行IP地址和域名的映射
}
}
端口
端口是虚拟的概念,并不是说在主机上真的有若干个端口。通过端口,可以在一个主机上运行多个网络应用程序。端口的表示是一 个16位的二进制整数,2个字节,对应十进制的0-65535。
Windows系统下
- 查看所有端口:netstat -ano
- 查看指定端口:netstat -aon|findstr “808”
- 查看指定进程:tasklist|findstr “808”
- 查看具体程序:使用任务管理器查看PID
使用InetSocketAddress
包含端口,用于socket通信的
import java.net.InetSocketAddress;
public class PortTest {
public static void main(String[] args) {
//包含端口
InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1",8080);
InetSocketAddress socketAddress2 = new InetSocketAddress("localhost",9000);
System.out.println(socketAddress.getHostName());
System.out.println(socketAddress.getAddress());
System.out.println(socketAddress2.getAddress());
System.out.println(socketAddress2.getPort());
}
}
URL
URI: Universal Resource Identifier 统一资源标志符,用 来标识抽象或物理资源的一个紧凑字符串。
URL:Universal Resource Locator 统一资源定位符,一种定 位资源的主要访问机制的字符串,一个标准的URL必须包括: protocol、host、port、path、parameter、anchor。
URN:Universal Resource Name 统一资源名称,通过特定命名 空间中的唯一名称或ID来标识资源。
URL类
统一资源定位符,由4部分组成:协议 、存放资源的主机域名、端口号和资源文件名。
import java.net.MalformedURLException;
import java.net.URL;
public class URLTest {
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("http://www.baidu.com:80/index.html?uname=shsxt&age=18#a");
//获取四个值
System.out.println("协议:"+url.getProtocol());
System.out.println("域名|ip:"+url.getHost());
System.out.println("端口:"+url.getPort());
System.out.println("请求资源1:"+url.getFile());
System.out.println("请求资源2:"+url.getPath());
//参数
System.out.println("参数:"+url.getQuery());
//锚点
System.out.println("锚点:"+url.getRef());
}
}
网络爬虫
获取数据
允许直接获取数据
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
public class SpiderTest01 {
public static void main(String[] args) throws Exception {
//获取URL
URL url =new URL("https://www.jd.com");
//URL url =new URL("https://www.dianping.com");
//下载资源
InputStream is = url.openStream();
BufferedReader br =new BufferedReader(new InputStreamReader(is,"UTF-8"));
String msg =null;
while(null!=(msg=br.readLine())) {
System.out.println(msg);
}
br.close();
}
}
报403,不允许直接获取数据
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
class SpiderTest02 {
public static void main(String[] args) throws Exception {
//获取URL
URL url =new URL("https://www.dianping.com");
//下载资源
HttpURLConnection conn =(HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
BufferedReader br =new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
String msg =null;
while(null!=(msg=br.readLine())) {
System.out.println(msg);
}
br.close();
}
}