Python采集易贝(eBay)商品详情API接口
要采集eBay商品详情,你可以使用eBay官方提供的API。以下是使用Python通过eBay Finding API获取商品详情的完整示例:
准备工作
- 注册账号并获取API密钥:
- 选择适合的API(如Finding API、Shopping API或Buy API)
示例代码(使用Finding API)
|-----------------------------------------------------------------------------|
| import requests
|
| import json
|
| |
| # eBay API配置
|
| EBAY_API_KEY = 'YOUR_EBAY_API_KEY' # 替换为你的eBay API密钥
|
| EBAY_APP_ID = 'YOUR_APP_ID' # 替换为你的App ID
|
| EBAY_ENDPOINT = 'https://svcs.ebay.com/services/search/FindingService/v1'
|
| |
| def get_ebay_item_details(item_id):
|
| """
|
| 通过eBay Finding API获取商品详情
|
| :param item_id: eBay商品ID
|
| :return: JSON格式的商品详情
|
| """
|
| headers = {
|
| 'X-EBAY-SOA-SECURITY-APPNAME': EBAY_APP_ID,
|
| 'X-EBAY-SOA-OPERATION-NAME': 'findItemsByProduct',
|
| 'X-EBAY-SOA-REQUEST-DATA-FORMAT': 'JSON',
|
| 'X-EBAY-SOA-RESPONSE-DATA-FORMAT': 'JSON',
|
| 'X-EBAY-SOA-GLOBAL-ID': 'EBAY-US', # 美国站点,可根据需要修改
|
| 'Content-Type': 'application/json'
|
| }
|
| |
| params = {
|
| 'productId.type': 'ReferenceID',
|
| 'productId': item_id,
|
| 'paginationInput.entriesPerPage': 1,
|
| 'itemFilter(0).name': 'Condition',
|
| 'itemFilter(0).value': 'New' # 只获取全新商品
|
| }
|
| |
| try:
|
| response = requests.get(
|
| EBAY_ENDPOINT,
|
| headers=headers,
|
| params=params
|
| )
|
| response.raise_for_status() # 检查请求是否成功
|
| |
| # 解析JSON响应
|
| data = response.json()
|
| |
| # 检查是否找到商品
|
| if 'findItemsByProductResponse' in data and \
|
| 'searchResult' in data['findItemsByProductResponse'][0] and \
|
| 'item' in data['findItemsByProductResponse'][0]['searchResult'][0]:
|
| |
| items = data['findItemsByProductResponse'][0]['searchResult'][0]['item']
|
| if items:
|
| return items[0] # 返回第一个商品详情
|
| |
| return {"error": "Item not found"}
|
| |
| except requests.exceptions.RequestException as e:
|
| return {"error": f"Request failed: {str(e)}"}
|
| except json.JSONDecodeError as e:
|
| return {"error": f"JSON parse failed: {str(e)}"}
|
| |
| # 使用示例
|
| if __name__ == "__main__":
|
| # 示例商品ID(替换为实际的eBay商品ID)
|
| item_id = "123456789012" # 这是一个示例ID,需要替换
|
| |
| item_details = get_ebay_item_details(item_id)
|
| print(json.dumps(item_details, indent=2, ensure_ascii=False))
|
替代方案:使用eBay Buy API(更现代)
eBay Buy API提供了更现代的接口来获取商品详情:
|------------------------------------------------------------|
| import requests
|
| import json
|
| |
| EBAY_API_KEY = 'YOUR_EBAY_API_KEY' # 替换为你的OAuth令牌
|
| EBAY_ENDPOINT = 'https://api.ebay.com/buy/item/v1/item/'
|
| |
| def get_item_details_buy_api(item_id):
|
| """
|
| 使用eBay Buy API获取商品详情
|
| """
|
| headers = {
|
| 'Authorization': f'Bearer {EBAY_API_KEY}',
|
| 'Accept': 'application/json'
|
| }
|
| |
| try:
|
| response = requests.get(
|
| f"{EBAY_ENDPOINT}{item_id}",
|
| headers=headers
|
| )
|
| response.raise_for_status()
|
| return response.json()
|
| except requests.exceptions.RequestException as e:
|
| return {"error": f"Request failed: {str(e)}"}
|
| |
| # 使用示例
|
| if __name__ == "__main__":
|
| item_id = "v1|123456789012|0" # Buy API可能需要特定格式的ID
|
| details = get_item_details_buy_api(item_id)
|
| print(json.dumps(details, indent=2, ensure_ascii=False))
|