【问题标题】:Shopify liquid pattern matching using Python使用 Python 进行 Shopify 液体模式匹配
【发布时间】:2018-02-16 12:08:20
【问题描述】:

在找到我在液体字符串中寻找的模式后,我想在 shopify 液体文件中添加一个新行(基本上它是一个带有一些变量的 HTML 文件)。

因为这是一个有效的 HTML 文件,我的意思是在不破坏 html 布局的情况下在 html 中添加新行作为新元素。

我正在考虑使用 BeautifulSoap 来加载字符串并进行操作,你觉得呢?

有没有更好的办法?

以上你会怎么做?

谢谢

【问题讨论】:

    标签: python beautifulsoup shopify


    【解决方案1】:

    Beautiful Soup 不会保持代码的对齐。您可以在 Beautiful Soup 中将代码对齐为空格或制表符,但它与以前不同。 Beautiful Soup 也会去除空白。我建议您进行字符串比较并替换您想要的任何位置。下面是一个示例,其中在 </body> 标记结束之前插入了 HTML 代码。

    #get the asset
    parsed_html = shopify.Asset.find('layout/theme.liquid').value
    
    #get the position where you want to insert the HTML
    index = parsed_html.find('</body>')
    
    #insert the HTML
    parsed_html = parsed_html[:index] + "\n\nYour HTML code\n\n" + parsed_html[index:]
    

    【讨论】:

      猜你喜欢
      • 2018-10-04
      • 1970-01-01
      • 2017-09-16
      • 1970-01-01
      • 2021-03-15
      • 2018-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多