Python 3.x 美丽的汤添加一个带有属性的标签并保存它
使用一个IntershopXML文件,我需要使用Python和BeautifulSoup在标记中添加以下标记 我想写的是:Python 3.x 美丽的汤添加一个带有属性的标签并保存它,python-3.x,beautifulsoup,attributes,tags,append,Python 3.x,Beautifulsoup,Attributes,Tags,Append,使用一个IntershopXML文件,我需要使用Python和BeautifulSoup在标记中添加以下标记 我想写的是: valueNouveau = soup.new_tag('custom-attribute', {"name" : "ImagesWEB"},{"xml:lang" : "fr-FR"}) valueNouveau.string = 'chaine' tagKeywords.append(val
valueNouveau = soup.new_tag('custom-attribute', {"name" : "ImagesWEB"},{"xml:lang" : "fr-FR"})
valueNouveau.string = 'chaine'
tagKeywords.append(valueNouveau)
当我这样做时,用msg写入文件时失败
unsupported operand type(s) for +: 'dict' and 'str' File
"/Users/cchauvin/Documents/Synonymes/Synoymes_etcetera.py", line 116,
in main f_output.write(str(soup))
当我写这篇文章时,一切都很顺利,我的文件也写了下来
valueNouveau = soup.new_tag('custom-Chauvin')
valueNouveau.string ='chaine'
tagKeywords.append(valueNouveau)
怎么了?我需要在现有的xml文件中添加这个标记
<custom-attribute name="ImagesWEB" dt:dt="string" xml:lang="fr-FR">
非常感谢您事先您不需要在唯一的字典中传递标记的每个属性,只需创建一个字典,并将所有需要的属性放入其中,然后将其传递到方法中。像这样:
dict_attributes = {"name" : "ImagesWEB",
"xml:lang" : "fr-FR"}
valueNouveau = soup.new_tag('custom-attribute',attrs=dict_attributes)
valueNouveau.string = 'chaine'
tagKeywords.append(valueNouveau)
输出:
<custom-attribute name="ImagesWEB" xml:lang="fr-FR">chaine</custom-attribute>
chaine