此脚本在运行时不会产生任何错误,但只会将单个项目插入数据库。如果我打印(imageUrl),则超过12项。
from aliexpress_api_client import AliExpress
from datetime import date, datetime, timedelta
import mysql.connector
cnx = mysql.connector.connect(user='root', password='Kradz579032!!',
host='127.0.0.1',
database='aliexpressapidb')
cursor = cnx.cursor()
add_data = ("INSERT INTO producttable "
"(productId, productTitle, salePrice, originalPrice, imageUrl) "
"VALUES (%s, %s, %s, %s, %s)")
aliexpress = AliExpress('9420', 'bazaarmaya')
data = aliexpress.get_product_list(['productId', 'productTitle', 'salePrice', 'originalPrice', 'imageUrl'], 'pillow')
for product in data['products']:
productId = product['productId']
productTitle = product['productTitle']
salePrice = product['salePrice']
originalPrice = product['originalPrice']
imageUrl = product['imageUrl']
data_insert = (productId, productTitle, salePrice, originalPrice, imageUrl)
cursor.execute(add_data, data_insert)
cnx.commit()
cursor.close()
cnx.close()
答案 0 :(得分:0)
您没有为forEach循环的每次迭代执行。
尝试将data_insert = (productId, productTitle, salePrice, originalPrice, imageUrl)
和cursor.execute(add_data, data_insert)
移动到循环中,如下所示:
from aliexpress_api_client import AliExpress
from datetime import date, datetime, timedelta
import mysql.connector
cnx = mysql.connector.connect(user='root', password='Kradz579032!!',
host='127.0.0.1',
database='aliexpressapidb')
cursor = cnx.cursor()
add_data = ("INSERT INTO producttable "
"(productId, productTitle, salePrice, originalPrice, imageUrl) "
"VALUES (%s, %s, %s, %s, %s)")
aliexpress = AliExpress('9420', 'bazaarmaya')
data = aliexpress.get_product_list(['productId', 'productTitle', 'salePrice', 'originalPrice', 'imageUrl'], 'pillow')
for product in data['products']:
productId = product['productId']
productTitle = product['productTitle']
salePrice = product['salePrice']
originalPrice = product['originalPrice']
imageUrl = product['imageUrl']
data_insert = (productId, productTitle, salePrice, originalPrice, imageUrl)
cursor.execute(add_data, data_insert)
cnx.commit()
cursor.close()
cnx.close()
那应该解决你的问题。