python自定义html_替换Python字符串中的自定义“HTML”标记

该博客介绍了如何利用pyparsing库的makeHTMLTags方法来解析特定的HTML标签,如'photo'。通过创建解析器并添加回调函数,可以获取原始标签、属性等信息。此外,还展示了如何进行标签替换,将'photo'标签转换为'div'。这有助于理解如何在Python中处理和操作HTML文档结构。
摘要由CSDN通过智能技术生成

对于这种“外科”分析(在这种分析中,您希望隔离特定的标记,而不是创建完整的分层文档),pyparsing的makeHTMLTags方法非常有用。在

请参阅下面带注释的脚本,其中显示了解析器的创建,并将其用于parseTag和{}方法:import pyparsing as pp

def make_tag_parser(tag):

# makeHTMLTags returns 2 parsers, one for the opening tag and one for the

# closing tag - we only need the opening tag; the parser will return parsed

# fields of the tag itself

tag_parser = pp.makeHTMLTags(tag)[0]

# instead of returning parsed bits of the tag, use originalTextFor to

# return the raw tag as token[0] (specifying asString=False will retain

# the parsed attributes and tag name as attributes)

parser = pp.originalTextFor(tag_parser, asString=False)

# add one more callback to define the 'raw' attribute, copied from t[0]

def add_raw_attr(t):

t['raw'] = t[0]

parser.addParseAction(add_raw_attr)

return parser

# parseTag to find all the matches and report their attributes

def parseTag(tag, s):

return make_tag_parser(tag).searchString(s)

content = """This is a string"""

tag_matches = parseTag("photo", content)

for match in tag_matches:

print(match.dump())

print("raw: {!r}".format(match.raw))

print("tag: {!r}".format(match.tag))

print("id: {!r}".format(match.id))

# transform tag to perform tag->div transforms

def replaceTag(tag, transform, s):

parser = make_tag_parser(tag)

# add one more parse action to do transform

parser.addParseAction(lambda t: transform.format(**t))

return parser.transformString(s)

print(replaceTag("photo",

'

{tag}_{id}
',

content))

印刷品:

^{pr2}$

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值