IP:

package com.sxt.loc;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* IP:定位一个节点:计算机、路由、通讯设备等
* InetAddress: 多个静态方法
* 1、getLocalHost:本机
* 2、getByName:根据域名DNS | IP地址 -->IP
*
* 两个成员方法
* 1、getHostAddress: 返回地址
* 2、getHostName:返回计算机名
*
*
*/
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.shsxt.com
//根据ip得到InetAddress对象
addr = InetAddress.getByName("61.135.169.121");
System.out.println(addr.getHostAddress()); //返回 baidu的ip:61.135.169.121
System.out.println(addr.getHostName()); //输出ip而不是域名。如果这个IP地 址不存在或DNS服务器不允许进行IP地址和域名的映射,
}
}
端口:
查看所有端口: netstat -ano
查看指定端口: netstat -ano|findstr "808"
查看指定进程: tasklist|finder "808"

package com.sxt.loc;
import java.net.InetSocketAddress;
/**
* 端口
* 1、区分软件
* 2、2个字节 0-65535 UDP TCP
* 3、同一个协议端口不能冲突
* 4、定义端口越大越好
* InetSocketAddress
* 1、构造器
* new InetSocketAddress(地址|域名,端口);
* 2、方法
* getAddress()
* getPort()
* getHostName()
* @author 裴新 QQ:3401997271
*
*/
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());
}
}
/*
*
192.168.60.255
admindeMacBook-Pro.local
61.135.169.121
www.baidu.com
61.135.169.121
61.135.169.121
*/
URL:

package com.sxt.loc;
import java.net.MalformedURLException;
import java.net.URL;
/**
* URL: 统一资源定位器,互联网三大基石之一(html http),区分资源
* 1、协议
* 2、域名、计算机
* 3、端口: 默认80
* 4、请求资源
* http://www.baidu.com:80/index.html?uname=shsxt&age=18#a
*
* @author
*
*/
public class URLTest01 {
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());
}
}
/**
*
协议:http
域名|ip:www.baidu.com
端口:80
请求资源1:/index.html?uname=shsxt&age=18
请求资源2:/index.html
参数:uname=shsxt&age=18
锚点:a
*
* */
抓取数据:

package com.sxt.loc;
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();
//分析
//处理。。。。
}
}

package com.sxt.loc;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* 网络爬虫的原理 +模拟浏览器
*
*
*
*/
public 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();
//分析
//处理。。。。
}
}
