我试图将csv文件中的数据获取到woocommerce API中,以插入新产品,但是我一直收到图像URL无效的错误。
错误: {'code':'woocommerce_product_image_upload_error','message':'获取远程图像时出错。错误:未提供有效的URL。.','data':{'status':400}}
THE CODE :
import requests
import json
from re import sub
from decimal import Decimal
import os
import csv
import time
from woocommerce import API
wcapi = API(
url="http://localhost:8000",
consumer_key="ck_8bd7cc64750f2393fc9b5b0ed...",
consumer_secret="cs_4d5ddae2b341c2721b9d4...",
wp_api=True,
timeout= 100,
version="wc/v2"
)
with open('AliExpress2CSV.csv', 'r') as f:
reader = csv.reader(f)
for row in reader:
categoryID = (row[0])
productitles = (row[2])
saleprices = (row[3])
images = (row[5]).replace("'","")[1:-1]
listimages = images.split(',')
# print(listimages)
longdesc = (row[6])
incr = Decimal(sub(r'[^\d.]', '', saleprices))
priceinc = incr * 3 # print(incr, priceinc)
conint = int(priceinc)
data = {
"name": productitles,
"type": "simple",
"regular_price": str(conint),
"description": 'longdesc',
"short_description": "",
"categories": [
{
"id": categoryID
}
],
"images": []
}
for number, url in enumerate(listimages):
data["images"].append({"src": url, "position": number})
print(wcapi.post("products", data).json())
print(data)
time.sleep(5)
输出:
{'name':' 2 在 1 多功能细点圆形细尖触摸屏笔电容式手写笔用于智能手机平板电脑适用于iPhone的iPad”,“类型”:“简单”,“常规价格”:“ 5”,“描述”:“ longdesc”,“ short_description”:”,“类别”:[{ id':'15'}],'images':[{'src':'https://ae01.alicdn.com/kf/HTB1D.TSMXXXXXcYXXXXq6xXFXXXH/2-in-1-Multifunction-Fine-Point-Round-Thin-Tip-Touch-Screen-Pen-Capacitive-Stylus-Pen-For.jpg','position':0},{'src':'https://ae01.alicdn.com/kf/HTB1.YrUMXXXXXchXXXXq6xXFXXXl/2-in-1-Multifunction-Fine-Point-Round-Thin-Tip-Touch-Screen-Pen-Capacitive-Stylus-Pen-For.jpg','position' :1},{'src':'https://ae01.alicdn.com/kf/HTB1q36ZMXXXXXX4XXXXq6xXFXXXO/2-in-1-Multifunction-Fine-Point-Round-Thin-Tip-Touch-Screen-Pen-Capacitive-Stylus-Pen-For.jpg','position':2},{'src':'https://ae01.alicdn.com/kf/HTB1n2HQMXXXXXadXpXXq6xXFXXXb/2-in-1-Multifunction-Fine-Point-Round-Thin-Tip-Touch-Screen-Pen-Capacitive-Stylus-Pen-For.jpg','position':3},{'src':' https://ae01.alicdn.com/kf/HTB19M2rMXXXXXapaXXXq6xXFXXXU/2-in-1-Multifunction-Fine-Point-Round-Thin-Tip-Touch-Screen-Pen-Capacitive-Stylus-Pen-For.jpg','position':4},{'src':'https://ae01.alicdn.com/kf/HTB1UtTCMXXXXXc.XFXXq6xXFXXXu/2-in-1-Multifunction-Fine-Point-Round-Thin-Tip-Touch-Screen-Pen-Capacitive-Stylus-Pen-For.jpg','position':5}]}}
您如何解决?