0

我目前正在使用 ebaysdk for python 向 eBay 查找 API 发出请求。每当我通常在第一页之后发出请求时,响应 JSON 不包含任何列表,如果我尝试从 JSON 字典中获取它,则会给我一个错误。

这是一些示例代码

from ebaysdk.finding import Connection as find_connect
 
verbs = ['findItemsAdvanced', 'findCompletedItems', 'findItemsByKeywords']
 
request = {
    'keywords': "Apple iPhone X 256GB",
    'itemFilter': [
        {'name': 'Condition', 'value': 'Used'},
        {'name': 'currency', 'value': 'CAD'},
        # {'name': 'minPrice', 'value': 100.0}
    ],
    'aspectFilter': [
        {
            'aspectName': 'Storage Capacity',
            'aspectValueName': '64 GB',
        }
    ],
    'categoryId': 9355,
    'paginationInput': {
        'entriesPerPage': 100,
        'pageNumber': 2
    },
    'sortOrder': 'BestMatch'
}
 
api = find_connect(config_file='../ebay_config.yaml', siteid="EBAY-ENCA")
listings = []
page_number = 1
 
while page_number <= 30:
    print(f'Page: {request["paginationInput"]["pageNumber"]}')
    response = api.execute(verbs[0], request).dict()
 
    page_number += 1
    try:
        listings.extend(response['searchResult']['item'])
        print(response)
    except KeyError as e:
        print(f"Key error occurred: attempted dictionary access json[{e}]")
        print(response)
    finally:  # after making a request on one page, increase the pageNumber by 1 to request the next page
        request['paginationInput']['pageNumber'] = page_number
    print(f'Number of Listings collected on page {page_number}: {len(listings)}', end='\n\n')
    listings.clear()

这是运行代码的输出

Page: 1 # the only one that worked
{'ack': 'Success', 'version': '1.13.0', 'timestamp': '2021-04-17T13:06:25.226Z', 'searchResult': {'item': [{'itemId': '174105091978', 'title': 'Apple iPhone X | 64GB 256GB | Unlocked Verizon AT&T T-Mobile Sprint | CDMA/GSM', 'globalId': 'EBAY-US', 'subtitle': 'FULLY TESTED | FREE SHIPPING | ACCESSORIES | WARRANTY', 'primaryCategory': {'categoryId': '9355', 'categoryName': 'Cell Phones & Smartphones'}, 'remaining listings removed for brevity'], '_count': '28'}, 'paginationOutput': {'pageNumber': '1', 'entriesPerPage': '100', 'totalPages': '4', 'totalEntries': '381'}, 'itemSearchURL': 'https://www.ebay.ca/sch/9355/i.html?LH_ItemCondition=2&_nkw=Apple+iPhone+X+256GB&Storage%2520Capacity%255Cu0007=64%2520GB&fsaspfilter=Storage%2520Capacity%3D64%2520GB&aspectrequest=1&_ddo=1&_ipg=100&_pgn=1&_sop=12'}
Number of Listings collected on page 2: 28
 
Page: 2
Key error occurred: attempted dictionary access json['item']
{'ack': 'Success', 'version': '1.13.0', 'timestamp': '2021-04-17T13:06:25.763Z', 'searchResult': {'_count': '0'}, 'paginationOutput': {'pageNumber': '2', 'entriesPerPage': '100', 'totalPages': '4', 'totalEntries': '381'}, 'itemSearchURL': 'https://www.ebay.ca/sch/9355/i.html?LH_ItemCondition=2&_nkw=Apple+iPhone+X+256GB&Storage%2520Capacity%255Cu0007=64%2520GB&fsaspfilter=Storage%2520Capacity%3D64%2520GB&aspectrequest=1&_ddo=1&_ipg=100&_pgn=2&_sop=12'}
Number of Listings collected on page 3: 0
 
Page: 3
Key error occurred: attempted dictionary access json['item']
{'ack': 'Success', 'version': '1.13.0', 'timestamp': '2021-04-17T13:06:26.222Z', 'searchResult': {'_count': '0'}, 'paginationOutput': {'pageNumber': '3', 'entriesPerPage': '100', 'totalPages': '4', 'totalEntries': '381'}, 'itemSearchURL': 'https://www.ebay.ca/sch/9355/i.html?LH_ItemCondition=2&_nkw=Apple+iPhone+X+256GB&Storage%2520Capacity%255Cu0007=64%2520GB&fsaspfilter=Storage%2520Capacity%3D64%2520GB&aspectrequest=1&_ddo=1&_ipg=100&_pgn=3&_sop=12'}
Number of Listings collected on page 4: 0
 
Page: 4
Key error occurred: attempted dictionary access json['item']
{'ack': 'Success', 'version': '1.13.0', 'timestamp': '2021-04-17T13:06:26.726Z', 'searchResult': {'_count': '0'}, 'paginationOutput': {'pageNumber': '4', 'entriesPerPage': '100', 'totalPages': '4', 'totalEntries': '381'}, 'itemSearchURL': 'https://www.ebay.ca/sch/9355/i.html?LH_ItemCondition=2&_nkw=Apple+iPhone+X+256GB&Storage%2520Capacity%255Cu0007=64%2520GB&fsaspfilter=Storage%2520Capacity%3D64%2520GB&aspectrequest=1&_ddo=1&_ipg=100&_pgn=4&_sop=12'}
Number of Listings collected on page 5: 0
4

0 回答 0