java网络编程作业+爬虫

 

 一、一种传接(没处理java.net拒绝连接,就这样吧)

客户端

package tt.tt.tt.oo;

import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

public class Client {
    public static void main(String[] args) {
        try{
            InetAddress ia=InetAddress.getByName("127.0.0.1");
            Socket s=new Socket(ia,9999);
            String str="这条消息来自客户端";
            OutputStream os=s.getOutputStream();
            os.write(str.getBytes());
            s.shutdownInput();
            os.close();
            s.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

服务器

package tt.tt.tt.oo;

import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) {
        try {
            ServerSocket ss=new ServerSocket(9999);
            System.out.println("启动了");
            Socket s=ss.accept();

            InputStream is=s.getInputStream();
            byte[] b=new byte[1024];
            int num=0;
            while ((num=is.read(b))!=-1){
                String str=new String(b,0,num);
                System.out.println(str);
            }
            is.close();
            s.close();
            ss.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

三、重点是<a></a>的获取

①前提:仅仅提<a>内容的代码 注意i=1起头 (注释的是保留<a>标签,注意i的 起终位置)

package tt.tt.tt.t3;

public class main {
    public static void main(String[] args) {
        String html="<div class=\"tbh-qr J_Module\" data-name=\"qr\" data-spm=\"201858\"><div class=\"qr\"><a href=\"https://market.m.taobao.com/app/fdilab/download-page/main/index.html\" class=\"qr-bd clearfix\"><span id=\"mtb\" class=\"h\">下载淘宝</span><img class=\"qrcode\" src=\"//g.alicdn.com/s.gif\" data-src=\"//gw.alicdn.com/imgextra/i4/O1CN01GZ8Gm51DHyo7asI5m_!!6000000000192-2-tps-160-160.png\" aria-labelledby=\"mtb\"/></a></div>";//存网页所以代码
        String[] split=html.split("<a");
        for(int i=1;i<split.length;i++){//是1不是0!
            String s=split[i];
            int start=s.indexOf(">");
            int end=s.indexOf("</a>");
            System.out.println(s.substring(start +1,end));
        }
//String[] split=html.split("</a>");
//        for(int i=0;i<split.length-1;i++){
//            String s=split[i];
//            int start=s.indexOf("<a");
//            System.out.println(s.substring(start,s.length())+"</a>");
//        }
    }
}

正解部分:

package tt.tt.tt.t3;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

public class urltest {
    public static void main(String[] args) throws IOException {
        URL url=new URL("https://www.taobao.com/");
        InputStream is=url.openStream();

        FileOutputStream fos=new FileOutputStream("E:/TEST/taobao.html");
        BufferedOutputStream bos=new BufferedOutputStream(fos);

        String html="";//存网页所以代码


        byte[] b=new byte[1024];
        int num=0;
        while ((num=is.read(b))!=-1){
            bos.write(b,0,num);
            String s=new String(b,0,num);
            System.out.println(s);
            html+=s;//存
        }
        String[] split=html.split("<a");
        for(int i=1;i<split.length;i++){
            String s=split[i];
            int start=s.indexOf(">");
            int end=s.indexOf("</a>");
            String str=s.substring(start +1,end);

            System.out.println(str.replace("\n","").trim());//剔除多余回车+两边多余的空格
        }
        bos.close();
        fos.close();
        is.close();
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牛大了2023

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值