admin
2025-02-25 30d8e227e8d823b6c38c3b9c90ac2df03b63befe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.yeshi.fanli.util;
 
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.yeshi.utils.HttpUtil;
import org.yeshi.utils.StringUtil;
 
import com.yeshi.fanli.dto.douyin.DouYinGoods;
 
import net.sf.json.JSONObject;
 
public class DouYinUtil {
 
    public static String parseDouYinGoods(String url) {
        HttpClient httpClient = new HttpClient();
        GetMethod gm = new GetMethod(url);
        try {
            httpClient.executeMethod(gm);
            String finalUrl = gm.getURI().toString();
            if (finalUrl.startsWith("https://s.click.taobao.com")) {
                Document doc = Jsoup.parse(gm.getResponseBodyAsString());
            
                Elements els = doc.getElementsByTag("meta");
                for (int i = 0; i < els.size(); i++) {
                    if (els.get(i).toString().contains("URL=")) {
//                        System.out.println(els.get(i)..toString());
                    }
                    System.out.println(els.get(i).attr("URL"));
                }
            }
 
        } catch (Exception e) {
        }
        return null;
    }
 
    
    /**
     * 通过链接获取商品标题,图片信息
     * @param url
     * @return
     */
    public static DouYinGoods getGoodsInfo(String url) {
        if (StringUtil.isNullOrEmpty(url)) 
            return null;
        
        try {
            String id = null;
            int indexOf = url.indexOf("?");
            url = url.substring(indexOf + 1, url.length()-1);
            String[] temp = url.split("&");
            for (int i =0; i < temp.length; i ++) {
                 String content = temp[i];
                if (content.startsWith("id")) {
                    id = content.split("=")[content.split("=").length-1];
                    break;
                } 
            }
            
            if (StringUtil.isNullOrEmpty(id)) 
                return null;
            
            // 请求地址
            String requestUrl = "https://ec.snssdk.com/product/fxgajaxstaticitem?b_type_new=0&id=%s";
            // 执行请求
            String result = HttpUtil.get(String.format(requestUrl,id));
            
            if (!StringUtil.isNullOrEmpty(result)) {
                JSONObject json = JSONObject.fromObject(result);
                JSONObject data = json.getJSONObject("data");
                
                DouYinGoods goods = new DouYinGoods();
                goods.setId(id);
                goods.setName(data.optString("name"));
                goods.setImg(data.optString("img"));
                return goods;
                
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}