心态快爆炸,获取商品ID已经困扰好几天了,网上搜寻的办法也总是达不到想要的效果,要么没有权限,要么获取不到Location。。
附上研究结果工具包(俺就喜欢各种工具包,简单快捷,有其他见解的同志欢迎骚扰沟通哈)
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.stylefeng.guns.core.util.SinataUtil;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.TbkItemInfoGetRequest;
import com.taobao.api.response.TbkItemInfoGetResponse;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
/**
* 获取淘宝商品信息 外网测试
* 1.喵有券申请appkey 并添加IP白名单 Get请求
* 2.淘宝开放平台申请应用 申请接口权限
*/
public class HttpURLConnectionExample {
//喵有券开放平台
private static String USER_AGENT = "Mozilla/5.0";
private static String appkey = "*****"; //需申请
//淘宝开放平台
private static String tbkurl = "http://gw.api.taobao.com/router/rest";
private static String tbkappkey = "*****"; //需申请
private static String tbksecret = "*****"; //需申请
/**
* 根据商品长链接|短连接获取商品ID HTTP GET请求
* @param requestUrl 长链接 | 短链接 例如:https://s.click.taobao.com/TGCXGxu
* @return 商品ID
*/
public static String getGoodsByUrl(String requestUrl) throws Exception {
String url = "http://api.web.21ds.cn/taoke/sclicktoid?apkey="+appkey+"&sclickurl="+requestUrl;
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//默认值我GET
con.setRequestMethod("GET");
//添加请求头
con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//打印结果
System.out.println("测试结果:"+response.toString());
JSONObject jsonObject = JSON.parseObject(response.toString());
if (SinataUtil.isNotEmpty(response)){
if ("200".equals(jsonObject.getString("code"))){
return jsonObject.getString("data");
}
}
return null;
}
/**
* 根据商品淘口令获取商品ID HTTP GET请求
* @param content 淘口令 例如:fu置本段内容$DHOJcXcVS7v$咑幵τao寶或點击链街https://m.tb.cn/h.VEtZL8M?sm=d68d09 至瀏lan嘂..【这个#聚划算团购#宝贝不错:金桔百香果蜂蜜柠檬片网红纯水果茶果干泡水花茶组合冷泡養生茶包(分享自@手机淘宝android客户端)】 或者 $DHOJcXcVS7v$
* @return 商品ID
*/
public static String getGoodsByKouLing(String content) throws Exception {
content = URLEncoder.encode(content,"utf-8");
String pid = "*****"; //需申请
String tbname = "*****"; //需申请
String url = "http://api.web.21ds.cn/taoke/doTpwdCovert?apkey="+appkey+"&pid="+pid+"&content="+content+"&tbname="+tbname;
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//默认值我GET
con.setRequestMethod("GET");
//添加请求头
con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//打印结果
System.out.println("测试结果:"+response.toString());
JSONObject jsonObject = JSON.parseObject(response.toString());
if (SinataUtil.isNotEmpty(response)){
if ("200".equals(jsonObject.getString("code"))){
JSONObject data = jsonObject.getJSONObject("data");
return data.getString("item_id");
}
}
return null;
}
/**
* 根据商品ID获取商品详情
* @param goodsId 商品ID
* @return 返回示例:"{\"tbk_item_info_get_response\":{\"results\":{\"n_tbk_item\":[{\"cat_leaf_name\":\"月饼\",\"cat_name\":\"零食\\/坚果\\/特产\",\"free_shipment\":true,\"item_url\":\"https:\\/\\/detail.tmall.com\\/item.htm?id=624313838549\",\"ju_online_end_time\":\"0\",\"ju_online_start_time\":\"0\",\"ju_pre_show_end_time\":\"0\",\"ju_pre_show_start_time\":\"0\",\"material_lib_type\":\"1\",\"nick\":\"五芳斋官方旗舰店\",\"num_iid\":624313838549,\"pict_url\":\"https:\\/\\/img.alicdn.com\\/bao\\/uploaded\\/i1\\/190903296\\/O1CN01tHa8gh1aDc39xTelC_!!190903296-0-lubanu-s.jpg\",\"presale_deposit\":\"0\",\"presale_end_time\":0,\"presale_start_time\":0,\"presale_tail_end_time\":0,\"presale_tail_start_time\":0,\"provcity\":\"浙江 嘉兴\",\"reserve_price\":\"199\",\"seller_id\":190903296,\"small_images\":{\"string\":[\"https:\\/\\/img.alicdn.com\\/i4\\/190903296\\/O1CN01NfeFYP1aDc38FNrnC_!!190903296.jpg\",\"https:\\/\\/img.alicdn.com\\/i3\\/190903296\\/O1CN01VQssyL1aDc2wvpZYs_!!190903296-0-lubanu-s.jpg\",\"https:\\/\\/img.alicdn.com\\/i1\\/190903296\\/O1CN015XyVX91aDc302jz4K_!!190903296-0-lubanu-s.jpg\",\"https:\\/\\/img.alicdn.com\\/i3\\/190903296\\/O1CN014INWLF1aDc3GCyt2z_!!190903296-2-lubanu-s.png\"]},\"title\":\"五芳斋月饼礼盒装蛋黄莲蓉豆沙月饼多口味散装团购送礼广式中秋节\",\"tmall_play_activity_end_time\":0,\"tmall_play_activity_start_time\":0,\"user_type\":1,\"volume\":237985,\"zk_final_price\":\"74.9\"}]},\"request_id\":\"6w9g7b4rxzeb\"}}"
*/
public static String getGoodsDetail(String goodsId) throws Exception {
TaobaoClient client = new DefaultTaobaoClient(tbkurl, tbkappkey, tbksecret);
TbkItemInfoGetRequest req = new TbkItemInfoGetRequest();
req.setNumIids(goodsId);
req.setPlatform(1L);
req.setIp("39.100.125.237");
TbkItemInfoGetResponse rsp = client.execute(req);
System.out.println(rsp.getBody());
return rsp.getBody();
}
}
可参考网站有:
https://open.taobao.com/api.htm?docId=24518&docType=2&scopeId=16189
https://open.21ds.cn/index/index/openapi/id/45.shtml?ptype=1
https://open.21ds.cn/index/index/openapi/id/87.shtml?ptype=1