【问题标题】:How to add a width attribute to each img tag using a django template filter?如何使用 django 模板过滤器为每个 img 标签添加宽度属性?
【发布时间】:2012-03-10 01:08:39
【问题描述】:

当在 rss 阅读器中显示我网站中某些文章的正文时,它们是全角的。我希望在 rss 提要中使用时为所有图像添加宽度属性,最好使用过滤器,因为我使用模板将正文排列在其他一些元素中。

我写了以下方法,作为测试:

try:
    _parser = minidom.Text()
    _parser.data = obj.body
    _xml = _parser.toxml(encoding='UTF-8')
    _return = minidom.parseString(_xml)
    _images = _return.getElementsByTagName('img')
    print "============= This is what I found: ============="
    #print _images
except ExpatError as (e):
    print "============= This is what I found: ============="
    print ErrorString(e.code)

但输出看起来像这样:

============= This is what I found: =============
syntax error
============= This is what I found: =============
not well-formed (invalid token)
============= This is what I found: =============
syntax error
============= This is what I found: =============
syntax error
============= This is what I found: =============
syntax error
============= This is what I found: =============
syntax error
(and so on, there are no working cases)

所以也许我的方法完全错误,希望有人能帮助我。

【问题讨论】:

    标签: html django parsing dom


    【解决方案1】:

    我认为您不能使用 XML 解析器解析所有有效的 HTML。

    请查看python html parsing了解各种html解析方式。

    【讨论】:

      猜你喜欢
      • 2011-03-10
      • 2017-10-09
      • 2021-07-11
      • 2013-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-16
      相关资源
      最近更新 更多