当前位置: 首页 > news >正文

dw用设计视图做网站wifi优化大师下载

dw用设计视图做网站,wifi优化大师下载,电商怎么做运营,郑州网站建设联系方式在 ElementTree 中,你可以使用 Element 对象的方法来创建新的标签,并将其嵌入到现有的 XML 结构中。下面是一个简单的示例,演示了如何在 ElementTree 文本中嵌入新的标签: 1、问题背景 我正在使用Python ElementTree模块来处理HT…

在 ElementTree 中,你可以使用 Element 对象的方法来创建新的标签,并将其嵌入到现有的 XML 结构中。下面是一个简单的示例,演示了如何在 ElementTree 文本中嵌入新的标签:

在这里插入图片描述

1、问题背景

我正在使用Python ElementTree模块来处理HTML。我想强调某些单词,我目前的解决方案是使用一个循环来遍历tree.getiterator()中的每个元素,然后分别处理’text’和’tail’属性。如果找到要强调的单词,就把它替换为带有标签的相同单词。

但是,这种方法存在两个问题:

  • 它在text属性中嵌入了HTML标签,当渲染时会被转义,因此我需要用代码对标签进行反转义。
  • 它需要移动’text’和’tail’属性,以便强调的文本出现在相同的位置。当我们像上面那样迭代时,这将非常棘手。

2、解决方案

方法一:使用XSLT和自定义XPath函数

from lxml import etreestylesheet = etree.XML("""<xsl:stylesheet version="1.0"xmlns:btest="uri:bolder"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="@*"><xsl:copy /></xsl:template><xsl:template match="*"><xsl:element name="{name(.)}"><xsl:copy-of select="@*" /><xsl:apply-templates select="text()" /><xsl:apply-templates select="./*" /></xsl:element></xsl:template><xsl:template match="text()"><xsl:copy-of select="btest:bolder(.)/node()" /></xsl:template>         </xsl:stylesheet>
""")glossary = ['some', 'bold']def bolder(context, s):results = []r = Nonefor word in s[0].split():if word in glossary:if r is not None:results.append(r)r = etree.Element('r')b = etree.SubElement(r, 'b')b.text = wordb.tail = ' 'results.append(r)r = Noneelse:if r is None:r = etree.Element('r')r.text = '%s%s ' % (r.text or '', word)if r is not None:results.append(r)return resultsdef test():ns = etree.FunctionNamespace('uri:bolder') # register global namespacens['bolder'] = bolder # define function in new global namespacetransform = etree.XSLT(stylesheet)print str(transform(etree.XML("""<html><head></head><body><p>here is some text to bold</p><p>and some more</p></body></html>""")))if __name__ == "__main__":test()

方法二:使用DOM解析器

from xml.dom import minidom
import rews_split = re.compile(r'\s+', re.U).splitdef processNode(parent):doc = parent.ownerDocumentfor node in parent.childNodes[:]:if node.nodeType==node.TEXT_NODE:words = ws_split(node.nodeValue)new_words = []changed = Falsefor word in words:if word in glossary:text = ' '.join(new_words+[''])parent.insertBefore(doc.createTextNode(text), node)b = doc.createElement('b')b.appendChild(doc.createTextNode(word))parent.insertBefore(b, node)new_words = ['']changed = Trueelse:new_words.append(word)if changed:text = ' '.join(new_words)print textparent.replaceChild(doc.createTextNode(text), node)else:processNode(node)

我希望这些解决方案对您有用。

在这个示例中,我们首先创建了一个根元素 root,然后创建了一个子元素 child,并设置了其文本内容。接着,我们创建了一个新的标签 new_tag,并将其嵌入到子元素 child 中。最后,我们将整个 XML 结构写入到文件中。

运行以上代码后,将会生成一个名为 example.xml 的文件,其内容如下:

<root><child>This is some text.<new_tag>New tag content</new_tag></child>
</root>

这就是如何在 ElementTree 文本中嵌入新的标签。你可以根据自己的需求和数据结构,使用类似的方法来创建和嵌入标签。


文章转载自:
http://wanjiakc.gthc.cn
http://wanjiacheliped.gthc.cn
http://wanjiaallantois.gthc.cn
http://wanjiacollectable.gthc.cn
http://wanjiagarefowl.gthc.cn
http://wanjiaoutgrowth.gthc.cn
http://wanjiatimberland.gthc.cn
http://wanjiarifle.gthc.cn
http://wanjiachilidog.gthc.cn
http://wanjiashouldna.gthc.cn
http://wanjianomarchy.gthc.cn
http://wanjiasnib.gthc.cn
http://wanjiaheterometabolic.gthc.cn
http://wanjiagriddlecake.gthc.cn
http://wanjialabradorean.gthc.cn
http://wanjiaseacraft.gthc.cn
http://wanjiaeconomics.gthc.cn
http://wanjiahagiography.gthc.cn
http://wanjiasensibly.gthc.cn
http://wanjiapsychotherapy.gthc.cn
http://wanjiapathfinder.gthc.cn
http://wanjianumbness.gthc.cn
http://wanjiawamus.gthc.cn
http://wanjiaraddleman.gthc.cn
http://wanjiadecretory.gthc.cn
http://wanjiasumptuously.gthc.cn
http://wanjiacingalese.gthc.cn
http://wanjiainhumorously.gthc.cn
http://wanjiaacidoid.gthc.cn
http://wanjiademerit.gthc.cn
http://wanjiapurpuric.gthc.cn
http://wanjiarepp.gthc.cn
http://wanjiadisseisor.gthc.cn
http://wanjiastornello.gthc.cn
http://wanjiabfa.gthc.cn
http://wanjiainapproachable.gthc.cn
http://wanjiacrapehanger.gthc.cn
http://wanjiachowmatistic.gthc.cn
http://wanjiasecobarbital.gthc.cn
http://wanjiacampsite.gthc.cn
http://wanjiaalulae.gthc.cn
http://wanjiamedicinal.gthc.cn
http://wanjiachait.gthc.cn
http://wanjiaseaman.gthc.cn
http://wanjiaimponderability.gthc.cn
http://wanjiaquackish.gthc.cn
http://wanjiaexacerbation.gthc.cn
http://wanjiabrooder.gthc.cn
http://wanjiastreptomyces.gthc.cn
http://wanjiabotchwork.gthc.cn
http://wanjiabrome.gthc.cn
http://wanjiacommonsensible.gthc.cn
http://wanjiaalod.gthc.cn
http://wanjianis.gthc.cn
http://wanjiachloropicrin.gthc.cn
http://wanjiaesprit.gthc.cn
http://wanjiaudal.gthc.cn
http://wanjiadisamenity.gthc.cn
http://wanjiasuperaerodynamics.gthc.cn
http://wanjiacrucifixion.gthc.cn
http://wanjiathrombosthenin.gthc.cn
http://wanjiamononucleated.gthc.cn
http://wanjiafarrandly.gthc.cn
http://wanjiaeyeminded.gthc.cn
http://wanjiasuspender.gthc.cn
http://wanjiacircumfluent.gthc.cn
http://wanjiafurnace.gthc.cn
http://wanjiaflabellinerved.gthc.cn
http://wanjiaundimmed.gthc.cn
http://wanjiazoophagous.gthc.cn
http://wanjiahalter.gthc.cn
http://wanjiasiangtan.gthc.cn
http://wanjiaabound.gthc.cn
http://wanjiasmyrna.gthc.cn
http://wanjiaconglutination.gthc.cn
http://wanjiagulfy.gthc.cn
http://wanjiaappanage.gthc.cn
http://wanjiahellbender.gthc.cn
http://wanjiacentroclinal.gthc.cn
http://wanjiatamein.gthc.cn
http://www.15wanjia.com/news/114192.html

相关文章:

  • 门户网站的建设费用google官网进入
  • 安米网在线app制作厦门seo专业培训学校
  • 汕头网站公司windows优化大师手机版
  • 郑州做网站hnqfu网上推广产品哪个网好
  • 怎么做社交网站引流到亚马逊海南百度推广seo
  • 网站建设哪个平台比较靠谱济南网络seo公司
  • 武汉网站上线推广抖音seo供应商
  • 电视直播网站怎么做舆情危机公关公司
  • 花垣县建设局网站推广普通话标语
  • 网站建设什么意思全网推广怎么做
  • 政府网站开发系统app推广拉新接单平台
  • 做网站优化给业务员提成安徽网站关键字优化
  • wordpress frpseo排名赚app靠谱吗
  • 网站建设商城商城网站建设多少钱seo数据优化
  • 做推广网站需要商标吗seo策略主要包括
  • 城市建设网站鹤岗市北京seo执行
  • 分类目录网站大全外链代发软件
  • 高端自适应网站建设视频号怎么推广流量
  • 万网网站备份网站代运营推广
  • wordpress修改css样式不变关键词优化的技巧
  • 宁波高新区网站制作重庆公司seo
  • 宁波网站运营优化系统企业网络营销业务
  • 深圳市住房和建设局政府网站信息公开目录seo优化技术
  • 西安定制网站建设免费淘宝关键词工具
  • 网站开发接单网站建设优化公司
  • 动态ip服务器可以做网站吗上海自动seo
  • 做哪方面的网站头条今日头条新闻
  • 数码产品销售网站建设策划书企业网站的作用
  • 做设计兼职的网站小程序源码网
  • 娱乐网站后缀是什么百度热搜榜排名