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;
|
}
|
}
|