淘宝商品详情描述API接口(taobao.item_get_desc)

淘宝商品描述API接口是淘宝开放平台提供的一个接口,用于获取某个商品的详细描述信息。

使用该接口,可以通过商品ID或商品链接获取商品的详细描述信息,包括商品的标题、图片、价格、销量、评价等。同时,还可以获取商品的规格参数、SKU信息、运费模板等。

Taobao.item_get_desc-获取淘宝商品详情描述API接口

接口请求的URL为:o0b.cn/opandy 

请求参数包括:商品ID或商品链接:可以使用淘宝商品ID或商品链接作为请求参数。

请求示例(复制Taobaoapi2014获取APISDK文件):

# coding:utf-8
"""
Compatible for python2.x and python3.x
requirement: pip install requests
"""
from __future__ import print_function
import requests
# 请求示例 url 默认请求参数已经做URL编码
url = "api-gw.xxx.cn/taobao/item_get_desc/?key=<您自己的apiKey>&secret=<您自己的apiSecret>&num_iid=520813250866"
headers = {
    "Accept-Encoding": "gzip",
    "Connection": "close"
}
if __name__ == "__main__":
    r = requests.get(url, headers=headers)
    json_obj = r.json()
    print(json_obj)

接口返回的数据包括:

  • 商品的详细描述信息:包括商品的标题、图片、价格、销量、评价等。
  • 商品的规格参数:包括商品的规格参数、SKU信息、运费模板等。

接口返回结果:

{
 		"item": {
		"desc": "\n  <img src=\"http://img.alicdn.com/imgextra/i3/2200639983103/O1CN01B8rO8q1YnDpqtaSHZ_!!2200639983103-1-scmitem6000.gif\" usemap=\"#IUSGU\" />\n  <map name=\"IUSGU\"><area shape=\"rect\" coords=\"6,10,786,541\" href=\"https://pages.tmall.com/wow/an/cs/act/wupr?wh_biz=tm&amp;wh_pid=3320144%2F17c73fe7b21&amp;disableNav=YES&amp;brandId=3320144&amp;chaoshi_brand_waitou=true\"></area><area shape=\"rect\" coords=\"9,561,169,731\" href=\"https://pages.tmall.com/wow/ark-pub/common/ee30c50a/tpl?wh_sid=a4a7a974fcf99970&amp;mcBrandCard=true\"></area><area shape=\"rect\" coords=\"211,561,371,721\" href=\"https://pages.tmall.com/wow/ark-pub/common/ee30c50a/tpl?wh_sid=b1f371c6cada937b&amp;mcBrandCard=true\"></area><area shape=\"rect\" coords=\"415,562,575,722\" href=\"https://pages.tmall.com/wow/ark-pub/common/ee30c50a/tpl?wh_sid=8fff1d96108c25a3&amp;mcBrandCard=true\"></area><area shape=\"rect\" coords=\"597,563,757,723\" href=\"https://pages.tmall.com/wow/ark-pub/common/ee30c50a/tpl?wh_sid=a4d5dbb8fee520eb&amp;mcBrandCard=true\"></area></map> \n  <img src=\"http://img.alicdn.com/imgextra/i3/2200639983103/O1CN01MdspcQ1YnDpofKaYI_!!2200639983103-1-scmitem6000.gif\" usemap=\"#EMBND\" />\n  <map name=\"EMBND\"><area shape=\"rect\" coords=\"3,253,355,792\" href=\"https://chaoshi.detail.tmall.com/item.htm?id=627358747270&amp;skuId=4958439969412\"></area></map> \n  <p>&nbsp;</p> \n  <p>&nbsp;</p> \n  <p>&nbsp;</p> \n  <p>&nbsp;</p> \n  <p>&nbsp;</p> \n  <p>&nbsp;</p> \n  <p>&nbsp;<img src=\"http://img.alicdn.com/imgextra/i4/2200639983103/O1CN01K5mRzF1YnDhfbLqYO_!!2200639983103-0-scmitem6000.jpg\" align=\"absmiddle\" /></p> \n  <p>&nbsp;</p> \n  <p><map name=\"GPSIJ\"><area shape=\"rect\" coords=\"194,8,374,148\" href=\"https://chaoshi.detail.tmall.com/item.htm?spm=a1z34w.13877023.0.0.626531a5VcC2US&amp;id=536597702698\"></area><area shape=\"rect\" coords=\"377,10,557,148\" href=\"https://chaoshi.detail.tmall.com/item.htm?spm=a1z34w.13877023.0.0.626531a5VcC2US&amp;id=611943282371\"></area><area shape=\"rect\" coords=\"564,15,742,150\" href=\"https://chaoshi.detail.tmall.com/item.htm?spm=a1z34w.13877023.0.0.626531a5VcC2US&amp;id=613199006000\"></area><area shape=\"rect\" coords=\"197,165,372,299\" href=\"https://chaoshi.detail.tmall.com/item.htm?spm=a1z34w.13877023.0.0.626531a5VcC2US&amp;id=602723508607\"></area><area shape=\"rect\" coords=\"382,170,554,305\" href=\"https://chaoshi.detail.tmall.com/item.htm?spm=a1z34w.13877023.0.0.626531a5VcC2US&amp;id=44107699067\"></area><area shape=\"rect\" coords=\"567,165,739,305\" href=\"https://chaoshi.detail.tmall.com/item.htm?spm=a1z34w.13877023.0.0.626531a5VcC2US&amp;id=44187888589\"></area><area shape=\"rect\" coords=\"197,318,374,455\" href=\"https://chaoshi.detail.tmall.com/item.htm?spm=a1z34w.13877023.0.0.626531a5VcC2US&amp;id=533938505960\"></area><area shape=\"rect\" coords=\"386,318,558,453\" href=\"https://chaoshi.detail.tmall.com/item.htm?spm=a1z34w.13877023.0.0.626531a5VcC2US&amp;id=591828039537\"></area><area shape=\"rect\" coords=\"567,318,741,460\" href=\"https://chaoshi.detail.tmall.com/item.htm?spm=a1z34w.13877023.0.0.626531a5VcC2US&amp;id=566408329269\"></area><area shape=\"rect\" coords=\"196,470,371,606\" href=\"https://chaoshi.detail.tmall.com/item.htm?spm=a1z34w.13877023.0.0.626531a5VcC2US&amp;id=533971548851\"></area><area shape=\"rect\" coords=\"380,473,557,610\" href=\"https://chaoshi.detail.tmall.com/item.htm?spm=a1z34w.13877023.0.0.626531a5VcC2US&amp;id=577183292025\"></area><area shape=\"rect\" coords=\"565,470,740,613\" href=\"https://chaoshi.detail.tmall.com/item.htm?spm=a1z34w.13877023.0.0.626531a5VcC2US&amp;id=573075979265\"></area><area shape=\"rect\" coords=\"197,622,262,676\" href=\"\"></area><area shape=\"rect\" coords=\"199,624,374,758\" href=\"https://chaoshi.detail.tmall.com/item.htm?spm=a1z34w.13877023.0.0.626531a5VcC2US&amp;id=554419816039\"></area><area shape=\"rect\" coords=\"381,620,556,762\" href=\"https://chaoshi.detail.tmall.com/item.htm?spm=a1z34w.13877023.0.0.626531a5VcC2US&amp;id=592956242653\"></area><area shape=\"rect\" coords=\"564,623,739,764\" href=\"https://chaoshi.detail.tmall.com/item.htm?spm=a1z34w.13877023.0.0.626531a5VcC2US&amp;id=604301910718\"></area></map> </p> \n  <p><a href=\"https://chaoshi.detail.tmall.com/item.htm?spm=a220o.7406545.0.0.32c7697bmJA9L1&amp;id=590048908797\" target=\"_blank\"></a></p> \n  <p><img src=\"http://img.alicdn.com/imgextra/i3/2200639983103/O1CN01UzEW4I1YnDhVdkEFJ_!!2200639983103-0-scmitem6000.jpg\" align=\"absmiddle\" /></p> \n  <p>&nbsp;</p> \n  <p><img src=\"http://img.alicdn.com/imgextra/i3/2200639983103/O1CN016eKqLt1YnDnXbDcWs_!!2200639983103-0-scmitem6000.jpg\" align=\"absmiddle\" /><img src=\"http://img.alicdn.com/imgextra/i2/2200639983103/O1CN01GDW3H61YnDnhPYxl7_!!2200639983103-0-scmitem6000.jpg\" align=\"absmiddle\" /><img align=\"absmiddle\" src=\"http://img.alicdn.com/imgextra/i2/3596645218/O1CN01seMU6n1oPt9fOvcj4_!!3596645218-0-scmitem6000.jpg\" /><img src=\"http://img.alicdn.com/imgextra/i2/2200639983103/O1CN0162Bmh21YnDnaPMaTM_!!2200639983103-0-scmitem6000.jpg\" align=\"absmiddle\" /><img align=\"absmiddle\" src=\"http://img.alicdn.com/imgextra/i3/3596645218/O1CN01f4fzGM1oPt9XJ7Hk2_!!3596645218-0-scmitem6000.jpg\" /><img align=\"absmiddle\" src=\"http://img.alicdn.com/imgextra/i4/3596645218/O1CN01rWjGD61oPt9fgZiT4_!!3596645218-0-scmitem6000.jpg\" /><img src=\"http://img.alicdn.com/imgextra/i2/2200639983103/O1CN01DfHuV61YnDjRoGnAQ_!!2200639983103-0-scmitem6000.jpg\" align=\"absmiddle\" /><img src=\"http://img.alicdn.com/imgextra/i3/2200639983103/O1CN01QZgCx01YnDjXm4Y03_!!2200639983103-0-scmitem6000.jpg\" align=\"absmiddle\" /><img align=\"absmiddle\" src=\"http://img.alicdn.com/imgextra/i3/2142811280/O1CN01WxEC6x1LKHSoigkL0_!!2142811280-0-scmitem6000.jpg\" /><img align=\"absmiddle\" src=\"http://img.alicdn.com/imgextra/i2/2200639983103/O1CN01CzM2ro1YnDUFRy4lH_!!2200639983103-0-scmitem6000.jpg\" /><img src=\"http://img.alicdn.com/imgextra/i3/2200639983103/O1CN01hy4eW41YnDnoTFBOb_!!2200639983103-0-scmitem6000.jpg\" align=\"absmiddle\" /></p> &nbsp; \n  <p>&nbsp;</p> \n  <p>&nbsp;</p>\n  <script src=\"https://g.alicdn.com/i/popshop/0.0.23/p/seemore/load.js?c\"></script> \n ",
		"data_from": "app_vip"
	},
	"error": "",
	"reason": "",
	"error_code": "0000",
	"cache": 0,
	"api_info": "today:24 max:10000 all[39=24+0+15];expires:2030-12-31",
	"execution_time": "0.666",
	"server_time": "Beijing/2023-06-21 09:18:44",
	"client_ip": "115.153.49.96",
	"call_args": [],
	"api_type": "taobao",
	"translate_language": "zh-CN",
	"translate_engine": "baidu",
	"server_memory": "0.86MB",
	"request_id": "gw-4.64924ff43c9b9",
	"last_id": "1821985889"
	}

使用淘宝商品描述API接口,可以方便地获取淘宝商品的详细描述信息,帮助开发者开发淘宝商品相关的应用。

### 使用 Python 编写淘宝商品详情数据的网页抓取程序 #### 准备工作 为了实现这一目标,需要准备几个重要的库来辅助完成任务。`requests`用于发送HTTP请求;`BeautifulSoup`或`lxml`解析HTML文档;可能还需要`selenium`处理JavaScript渲染的内容。 由于淘宝网站具有较强的反爬机制[^2],因此建议采用更高级的方法如Selenium模拟浏览器行为,并配合ChromeDriver或其他驱动器一起使用。 #### 设置环境变量与安装依赖包 确保已正确配置好Python环境并安装必要的第三方库: ```bash pip install requests beautifulsoup4 selenium pandas ``` 对于某些情况下动态加载的数据,则需考虑利用API接口直接获取JSON格式返回值而不是通过传统的HTML解析方式提取信息。 #### 获取页面源码 下面是一个简单的例子展示如何设置headers伪装成正常访问者以及初始化WebDriver对象打开指定URL地址: ```python from selenium import webdriver import time options = webdriver.ChromeOptions() # 添加header头文件防止被识别为自动化工具 options.add_argument('user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64)"') driver_path = r'path_to_chromedriver.exe' browser = webdriver.Chrome(executable_path=driver_path, options=options) url = 'https://item.taobao.com/item.htm?id={}'.format(product_id) browser.get(url) time.sleep(3) # 等待页面加载完毕 html_content = browser.page_source ``` #### 解析所需字段 根据实际需求选取合适的解析方法,这里给出部分常用属性的选择路径作为参考: - 宝贝ID:通常位于URL中; - 商品名称:可通过XPath定位到标题标签; - 销售价&原价:查找特定class名下的span元素; - 库存情况:同上,注意不同店铺显示形式有所差异; - 图片链接:一般存在于img标签src属性内; - 描述文字:正文区域内的p段落或者div容器。 示例代码如下所示: ```python from bs4 import BeautifulSoup def parse_product_info(html): soup = BeautifulSoup(html,'lxml') title = soup.find('h3', class_='tb-title').text.strip() if soup.find('h3', class_='tb-title') else None price_tag = soup.select_one('.price.g_price.g_price-highlight strong') current_price = float(price_tag.text.replace('¥','')) if price_tag else None original_price_element = soup.select_one('.origin-price') origin_price = float(original_price_element['data-original']) if original_price_element and 'data-original' in original_price_element.attrs else None stock_status = int(soup.select_one('#J_SpanStock').get_text()) if soup.select_one('#J_SpanStock') else None main_image_url = soup.select_one('#J_ImgBooth')['src'] if soup.select_one('#J_ImgBooth') else None description_elements = soup.select('.description p') descriptions = '\n'.join([desc.get_text().strip() for desc in description_elements]) if description_elements else '' return { "title": title, "current_price": current_price, "original_price": origin_price, "stock": stock_status, "main_image": f'https:{main_image_url}' if main_image_url.startswith('//') else main_image_url, "descriptions": descriptions } ``` 请注意以上CSS选择器和XPATH表达式可能会因为页面结构调整而失效,在正式项目里应该更加健壮地处理异常状况并且定期维护更新查询语句以适应变化后的DOM结构。 考虑到淘宝的安全策略非常严格,即使采取上述措施也不能完全规避风险。如果遇到验证码或者其他阻碍因素时,应当适当调整频率、增加随机等待间隔甚至更换IP等方式降低触发防护的可能性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值