C#代码
淘宝API C#.NET SDK地址
http://open.taobao.com/doc/detail.htm?id=112
引用:
using Top.Api;
using Top.Api.Request;
using Top.Api.Response;
using Top.Api.Domain;
using System.Reflection;
using System.Collections.Generic;
代码:
string url = "http://gw.api.taobao.com/router/rest";
string appkey = "*******";
string appsecret = "***********************************";
protected void Page_Load(object sender, EventArgs e)
{
ITopClient client = new DefaultTopClient(url, appkey, appsecret);
TaobaokeItemsGetRequest req = new TaobaokeItemsGetRequest();
req.Fields = "num_iid,click_url";
req.Nick = "dapeng031115";
req.PageNo = 1;
req.Keyword = "折扣";
TaobaokeItemsGetResponse response = client.Execute(req);
int l;
if (response.TotalResults < 5)
{
l = Convert.ToInt16(response.TotalResults);
}
else
{
l = 5;
}
List<TaobaokeItem> tbis = response.TaobaokeItems;
string[] LieMing = { "ClickUrl", "NumIid", "PicUrl", "Price", "Title" };
DataTable dt = new DataTable();
for (int i = 0; i < LieMing.Length; i++)
{
dt.Columns.Add(LieMing[i].ToString(), typeof(string));
}
for (int i = 0; i < l; i++)
{
DataRow dr = dt.NewRow();
dr["ClickUrl"] = tbis[i].ClickUrl.ToString();
Item tbdi = DetailProduct(tbis[i].NumIid.ToString());
for (int j = 2; j < LieMing.Length; j++)
{
//用属性名称取得类中属性的值
PropertyInfo ProInfo = tbdi.GetType().GetProperty(LieMing[j].ToString());
dr[LieMing[j]] = ProInfo.GetValue(tbdi, null).ToString();
}
dt.Rows.Add(dr);
}
Repeater1.DataSource = dt;
Repeater1.DataBind();
}
//根据商品ID获得商品详情
public Item DetailProduct(string numiid)
{
string sessionKey = "";
ITopClient client = new DefaultTopClient(url, appkey, appsecret);
ItemGetRequest reqd = new ItemGetRequest();
reqd.Fields = "detail_url,num_iid,title,nick,type,cid,seller_cids,props,input_pids,input_str,desc,pic_url,num,valid_thru,list_time,delist_time,stuff_status,location,price,post_fee,express_fee,ems_fee,has_discount,freight_payer,has_invoice,has_warranty,has_showcase,modified,increment,approve_status,postage_id,product_id,auction_point,property_alias,item_img,prop_img,sku,video,outer_id,is_virtual";
reqd.NumIid = Convert.ToInt64(numiid);
ItemGetResponse responsed = client.Execute(reqd, sessionKey);
Item tbdi = responsed.Item;
return tbdi;
}
protected string getsubstring(string s, int l)
{
string a = "";
if (s.Length < l + 1)
{ a = s; }
else
{
a = s.Substring(0, l) + "...";
}
return a;
}
- Python 示例:
import requests def get_discount_products(keyword, app_key): url = "https://api.taobao.com/rest/api3.do" params = { "method": "taobao.tbk.dg.material.optional", "app_key": app_key, "q": keyword, "pageSize": 10, # 返回结果的数量,可根据需求调整 "pageNo": 1 # 页码,可根据需求调整 } try: response = requests.get(url, params=params) result = response.json() if result.get("code") == 200: return result.get("result", {}).get("list", ()) else: print("请求失败:", result.get("msg")) return () except Exception as e: print("请求出错:", str(e)) return () # 使用示例 keyword = "手机" # 替换为你要搜索的关键字 app_key = "your_app_key" # 替换为你的淘宝开放平台应用的App Key discount_products = get_discount_products(keyword, app_key) for product in discount_products: print(product.get("title")) # 输出商品标题