You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Shopify API导入产品时描述格式与源站不一致问题排查

问题

我使用Shopify Admin REST API(2022-10版本)向Shopify账户添加产品时,产品描述的格式和LightInTheBox源站的对应产品描述不一致。目前只有Highlights部分显示正常,Selling Point和Specifications部分格式异常。

以下是我的实现代码:

description = soup.find("div",class_="description-container")
description = description.find_all("div")
highlights = ''
selling_point = ''
specifications = ''
for desc in description:
    try:
        if desc["data-sort"]=="13":
            highlights = desc
        if desc["data-sort"]=="14":
            selling_point = desc
        if desc["data-sort"]=="15":
            specifications = desc
    except:
        continue
payload ={
    "product":
    {
      "title": "aaaa1",
      "price": price,
      "body_html": str(highlights)+str(selling_point)+str(specifications),
      "sku":sku,
      "images":original_images,
      "product_type": "Cycling",
      "tags":tags}}
问题原因与解决方案

问题原因

  1. 直接将BeautifulSoup的Tag对象转为字符串时,会包含该Tag本身的外层容器(比如带有data-sort属性的div),这些多余的容器标签或属性会干扰Shopify对HTML的渲染,导致格式异常。
  2. 源站的Selling Point和Specifications部分的HTML结构可能更复杂,直接拼接Tag字符串可能导致标签嵌套错误、样式丢失,而Highlights部分恰好结构简单所以显示正常。

解决方案

1. 提取Tag内部的纯HTML内容

使用BeautifulSoup的.decode_contents()方法,获取Tag内部的完整HTML代码,而不是包含外层容器的字符串,避免多余标签干扰。

2. 清理冗余属性(可选)

可以移除源站中用于排序的data-sort等无关属性,让HTML更干净。

3. 添加结构化标题(可选)

给每个部分添加标题标签(如<h3>),让Shopify展示的结构更清晰。

修改后的代码示例

提取描述的代码:

description = soup.find("div", class_="description-container")
description_divs = description.find_all("div")
highlights_html = ''
selling_point_html = ''
specifications_html = ''

for desc in description_divs:
    try:
        if desc.get("data-sort") == "13":
            # 提取内部HTML,添加标题标签
            highlights_html = f"<h3>Highlights</h3>{desc.decode_contents()}"
        elif desc.get("data-sort") == "14":
            selling_point_html = f"<h3>Selling Points</h3>{desc.decode_contents()}"
        elif desc.get("data-sort") == "15":
            specifications_html = f"<h3>Specifications</h3>{desc.decode_contents()}"
    except Exception:
        continue

构造payload的代码:

payload = {
    "product": {
        "title": "aaaa1",
        "price": price,
        "body_html": highlights_html + selling_point_html + specifications_html,
        "sku": sku,
        "images": original_images,
        "product_type": "Cycling",
        "tags": tags
    }
}

内容的提问来源于stack exchange,提问作者etel876

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 10:05:23