使用Ebay Finding API获取商品均价时返回空响应的问题
问题
我是编程新手,想要编写代码获取Ebay平台上商品的平均售价。我选择使用Finding API,完成了如下代码实现:
import requests def get_average_selling_price(research_title, num_sales): #url = "https://svcs.ebay.com/services/search/FindingService/v1" url = "https://svcs.sandbox.ebay.com/services/search/FindingService/v1" headers = { "X-EBAY-SOA-SERVICE-VERSION": "1.0.0", "X-EBAY-SOA-GLOBAL-ID" : "EBAY-FR", "X-EBAY-SOA-SECURITY-APPNAME": APP-ID, "X-EBAY-SOA-RESPONSE-DATA-FORMAT": "JSON", "REST-PAYLOAD": "true", } params = { "X-EBAY-SOA-OPERATION-NAME": "findItemsByKeywords", "keywords": research_title, "itemFilter(0).name": "SoldItemsOnly", "itemFilter(0).value": "true", "paginationInput.entriesPerPage": num_sales, } try: response = requests.get(url, headers=headers, params=params) response.raise_for_status() #print(response.text) result = response.json() if "findItemsByKeywordsResponse" in result: items = result["findItemsByKeywordsResponse"][0].get("searchResult", [{}])[0].get("item", []) if len(items) == 0: print("No sales found for the specified item.") return total_price = sum(float(item["sellingStatus"][0]["convertedCurrentPrice"][0]["__value__"]) for item in items) average_price = total_price / len(items) print(f"Average Selling Price: ${average_price:.2f}") else: print("Invalid response received from the eBay API.") except requests.exceptions.RequestException as e: print("An error occurred during API requests:") print(e) # Example usage research_title = input("Enter the research title: ") num_sales = int(input("Enter the number of sales to consider: ")) get_average_selling_price(research_title, num_sales)
但运行时无论输入什么内容,都会触发“No sales found for the specified item”的错误处理逻辑。API返回的原始响应内容如下:
{"findItemsByKeywordsResponse":[{"ack":["Success"],"version":["1.13.0"],"timestamp":["2023-07-05T13:04:08.280Z"],"searchResult":[{"@count":"0"}],"paginationOutput":[{"pageNumber":["0"],"entriesPerPage":["5"],"totalPages":["0"],"totalEntries":["0"]}],"itemSearchURL":["https://www.ebay.com/sch/i.html?_nkw=noctali&_ddo=1&_ipg=5&_pgn=1"]}]}
不清楚为何响应为空且无条目返回,希望得到帮助排查问题。
排查与解决
沙箱环境数据不足:当前使用的是Ebay沙箱环境,该环境仅提供少量测试数据,基本没有已售出的商品记录,因此无论搜索什么关键词都会返回空结果。解决方式是注释掉沙箱URL,启用正式环境地址:
https://svcs.ebay.com/services/search/FindingService/v1。全局ID与站点不匹配:设置的全局ID为
EBAY-FR(法国站点),若搜索的关键词是其他站点的热门商品,可能在法国站点无匹配的已售记录。可尝试更换为EBAY-US(美国站点),或根据目标搜索站点调整全局ID。API权限限制:部分基础Ebay API权限不支持访问已售出商品数据,需要检查开发者账号是否开启了对应访问权限。
关键词问题:若关键词过于冷门或存在拼写错误,也会导致无结果返回。建议先在对应Ebay站点网页端手动搜索该关键词,确认存在已售商品后,再用相同关键词测试API。
内容的提问来源于stack exchange,提问作者Lazyx
相关产品推荐
相关产品推荐

