Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在BeautifulSoup中将CDATA NavigableStrings替换为标记_Python_Xml_Xml Parsing_Beautifulsoup_Cdata - Fatal编程技术网

Python 在BeautifulSoup中将CDATA NavigableStrings替换为标记

Python 在BeautifulSoup中将CDATA NavigableStrings替换为标记,python,xml,xml-parsing,beautifulsoup,cdata,Python,Xml,Xml Parsing,Beautifulsoup,Cdata,我正在使用BeautifulSoup解析几个XML文档提要,并希望进行一些预处理,以将非标准的CDATA标记替换为自定义XML标记。举例说明: 以下XML源代码 <title>The end of the world as we know it</title> <category><![CDATA[Planking Dancing]]></category> <pubDate><![CDATA[Sun, 16 Sep

我正在使用BeautifulSoup解析几个XML文档提要,并希望进行一些预处理,以将非标准的
CDATA
标记替换为自定义XML标记。举例说明:

以下XML源代码

<title>The end of the world as we know it</title>
<category><![CDATA[Planking Dancing]]></category>
<pubDate><![CDATA[Sun, 16 Sep 2012 12:00:00 EDT]]></pubDate>
<dc:creator><![CDATA[Bart Simpson]]></dc:creator>
我们所知道的世界末日
…将变成:

<title>The end of the world as we know it</title>
<category><myTag>Planking Dancing<myTag></category>
<pubDate><myTag>Sun, 16 Sep 2012 12:00:00 EDT<myTag></pubDate>
<dc:creator><myTag>Bart Simpson<myTag></dc:creator>
我们所知道的世界末日
跳板舞
2012年9月16日星期日美国东部时间12:00:00
巴特·辛普森
我认为这个问题以前没有人问过,所以(我尝试了一些不同的SO查询)。我还使用
.findAll('cdata',text=True)
尝试了几种不同的方法,并将BeautifulSoup方法应用于每个生成的
Navigablesting
。我所做的尝试要么没有替换,要么看起来像一个递归循环


我很高兴发布我以前的尝试,但鉴于这里的问题非常简单,我希望有人能发布一个清晰的示例,说明如何使用BeautifulSoup 3完成上述搜索和替换。

CData
Navigablesting
的子类,因此您可以找到所有
CData
元素,首先搜索所有
navigablesting
对象,然后进行测试 是否每个都是CData的实例。一旦你有了一个,就很容易了 按照您的建议,使用
replacetwith
替换:

>>> from BeautifulSoup import BeautifulSoup, CData, Tag
>>> source = """
... <title>The end of the world as we know it</title>
... <category><![CDATA[Planking Dancing]]></category>
... <pubDate><![CDATA[Sun, 16 Sep 2012 12:00:00 EDT]]></pubDate>
... <dc:creator><![CDATA[Bart Simpson]]></dc:creator>
... """
>>> soup = BeautifulSoup(source)
>>> for navstr in soup(text=True):
...     if isinstance(navstr, CData):
...         tag = Tag(soup, "myTag")
...         tag.insert(0, navstr[:])
...         navstr.replaceWith(tag)
... 
>>> soup

<title>The end of the world as we know it</title>
<category><myTag>Planking Dancing</myTag></category>
<pubdate><myTag>Sun, 16 Sep 2012 12:00:00 EDT</myTag></pubdate>
<dc:creator><myTag>Bart Simpson</myTag></dc:creator>

>>>
>>从BeautifulSoup导入BeautifulSoup、CData、标记
>>>来源=”“
…我们所知道的世界末日
... 
... 
... 
... """
>>>汤=美汤(来源)
>>>对于soup中的navstr(text=True):
...     如果isinstance(navstr,CData):
...         标签=标签(汤,“我的标签”)
...         tag.insert(0,navstr[:])
...         navstr.replaceWith(标记)
... 
>>>汤
我们所知道的世界末日
跳板舞
2012年9月16日星期日美国东部时间12:00:00
巴特·辛普森
>>>
几点注意:

  • 您可以像调用函数一样调用
    BeautifulSoup
    对象,并且 效果与调用其
    .findAll()
    方法相同

  • 我所知道的在BS3中获取
    CData
    对象内容的唯一方法是切片 如上面的代码片段所示
    str(navstr)
    将保留所有
    垃圾,这显然是你不想要的。在BS4中,
    str(navstr)
    给你的内容没有垃圾