获取淘宝淘口令的商品id

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 获取淘宝淘口令的商品id
 * @Author yanghouqing
 * @Date 2023/3/23
 */
public class TaoKouLingJieXi {

    public static void main(String[] args) throws Exception {
        // 提取淘口令中的短链
        List<String> urls = getUrls("【淘宝】https://m.tb.cn/h.Ur4aB3x?tk=wWfIdQOINbP CZ0001 「宝奈公司前台定制背景墙会议室办公室场间无缝竹木纤维集成墙板」\n" +
                "点击链接直接打开 或者 淘宝搜索直接打开");
        // 请求短链
        String s = doGet(urls.get(0));
        // 苹果url解析
        String validGoodsId = getGoodsIdFromUrl(getUrls(s).get(0));
        if(validGoodsId == null){
            // 安卓url解析
            validGoodsId = getAnZhuoGoodsIdFromUrl(getUrls(s).get(0));
        }
        System.out.println("商品id:"+validGoodsId);
    }

    /**
     * 获取安卓淘口令中的商品id
     * @param url
     * @return
     */
    public static  String getAnZhuoGoodsIdFromUrl(String url) {
        try{
            String goodsId =  url.substring(url.indexOf(".com/i")+6,url.indexOf(".htm?"));
            if(NumberUtils.toLong(goodsId,0) > 0){
                return  goodsId;
            }
        }catch (Exception e){}
        return  null;
    }

    /**
     * 获取苹果淘口令中的商品id
     * @param url
     * @return
     */
    public static String getGoodsIdFromUrl(String url) {
        try{
            url = url.substring( url.indexOf("?") + 1, url.length()).trim();
            String[] arr = url.split("&");
            for (int i = 0; i < arr.length; i++) {
                String[] arr2 = arr[i].split("=");
                if( arr2.length == 2 && StringUtils.equalsIgnoreCase(arr2[0],"id")){
                    String goodsId =  arr2[1];
                    if(NumberUtils.toLong(goodsId,0) > 0){
                        return  goodsId;
                    }
                }
            }
        }catch (Exception e){}
        return  null;
    }

    /**
     * 以get方式调用对方接口方法
     * @param pathUrl
     */
    public static String doGet(String pathUrl){
        BufferedReader br = null;
        String result = "";
        try {
            URL url = new URL(pathUrl);

            //打开和url之间的连接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            //设定请求的方法为"GET",默认是GET
            //post与get的不同之处在于post的参数不是放在URL字串里面,而是放在http请求的正文内。
            conn.setRequestMethod("GET");

            //设置30秒连接超时
            conn.setConnectTimeout(30000);
            //设置30秒读取超时
            conn.setReadTimeout(30000);

            // 设置是否向httpUrlConnection输出,因为这个是post请求,参数要放在http正文内,因此需要设为true, 默认情况下是false;
            conn.setDoOutput(true);
            // 设置是否从httpUrlConnection读入,默认情况下是true;
            conn.setDoInput(true);

            // Post请求不能使用缓存(get可以不使用)
            conn.setUseCaches(false);

            //设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");  //维持长链接
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");

            //连接,从上述url.openConnection()至此的配置必须要在connect之前完成,
            conn.connect();

            /**
             * 下面的代码相当于,获取调用第三方http接口后返回的结果
             */
            //获取URLConnection对象对应的输入流
            InputStream is = conn.getInputStream();
            //构造一个字符流缓存
            br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String str = "";
            while ((str = br.readLine()) != null){
                result += str;
            }
            System.out.println(result);
            //关闭流
            is.close();
            //断开连接,disconnect是在底层tcp socket链接空闲时才切断,如果正在被其他线程使用就不切断。
            conn.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (br != null){
                    br.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 提取淘口令中的短链
     * @param str 淘口令
     * @return 短链集
     */
    public static List<String> getUrls(String str) {
        Pattern pattern = Pattern.compile("(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]");
        Matcher matcher = pattern.matcher(str);
        List<String> urlArr = new ArrayList<>();
        while (matcher.find()) {
            urlArr.add(matcher.group());
        }
        return urlArr;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值