我尝试创建这样的循环,但它给了我错误
for prod in data:
productId = prod['productId']
错误:
productId = prod['productId']
TypeError: string indices must be integers
{'volume': '1', 'productId': 32702747712, 'evaluateScore': '4.9', 'storeUrl': 'https://www.aliexpress.com/store/1393300', 'lotNum': 1, 'discount': '48%', 'validTime': '2018-06-21', 'storeName': 'MORAZORA', 'productUrl': 'https://www.aliexpress.com/item/fashion-2017-hot-sale-top-quality-pu-nubuck-leather-ankle-boots-med-heel-round-toe-solid/32702747712.html', 'packageType': 'piece', 'allImageUrls': 'https://ae01.alicdn.com/kf/HTB10rBClEUIL1JjSZFrq6z3xFXaq/MORAZORA-Fashion-2018-hot-sale-top-quality-flock-ankle-boots-for-women-autumn-winter-high-heel.jpg,https://ae01.alicdn.com/kf/HTB129b2e_J_SKJjSZPiq6z3LpXaR/MORAZORA-Fashion-2018-hot-sale-top-quality-flock-ankle-boots-for-women-autumn-winter-high-heel.jpg,https://ae01.alicdn.com/kf/HTB1gYNClEUIL1JjSZFrq6z3xFXaJ/MORAZORA-Fashion-2018-hot-sale-top-quality-flock-ankle-boots-for-women-autumn-winter-high-heel.jpg,https://ae01.alicdn.com/kf/HTB1s04klC7PL1JjSZFHq6AciXXa0/MORAZORA-Fashion-2018-hot-sale-top-quality-flock-ankle-boots-for-women-autumn-winter-high-heel.jpg,https://ae01.alicdn.com/kf/HTB1AcUgdaagSKJjy0Faq6z0dpXa3/MORAZORA-Fashion-2018-hot-sale-top-quality-flock-ankle-boots-for-women-autumn-winter-high-heel.jpg,https://ae01.alicdn.com/kf/HTB1TYjLbu7EWeJjSZFMq6x00FXa7/MORAZORA-Fashion-2018-hot-sale-top-quality-flock-ankle-boots-for-women-autumn-winter-high-heel.jpg'}
如何创建prod:
data = aliexpress.get_product_details(['productId', 'productUrl', 'discount', 'evaluateScore',
'volume', 'packageType', 'lotNum', 'validTime', 'storeName', 'storeUrl', 'allImageUrls'], productId)
dmp = json.dumps(data)
for prod in dmp:
print(prod)
productId = prod['productId']
productUrl = prod['productUrl']
discount = prod['discount']
evaluateScore = prod['evaluateScore']
volume = prod['volume']
packageType = prod['packageType']
lotNum = prod['lotNum']
validTime = prod['validTime']
storeName = prod['storeName']
storeUrl = prod['storeUrl']
allImageUrls = prod['allImageUrls']
答案 0 :(得分:0)
这个问题来自于尝试遍历字典的键:
for prod in data:
print(prod)
返回..
'volume'
'productId'
'evaluateScore'
...etc
当你要求'volume'['productId']
Python变得困惑时 - 字符串支持整数索引,但字符串'productId'
对解释器没有任何意义。解决方案是将该dict放入列表中,或者直接使用prod['productId']