解析非标准XML(CDATA标记)

时间:2022-12-01 13:15:31

When I want to parsing XML document in Python using BeautifulSoup library, I faced some problems. The XML document that I want to parse:

当我想使用BeautifulSoup库在Python中解析XML文档时,我遇到了一些问题。我要解析的XML文档:

<item>
<title><![CDATA[Title Sample]]></title>
<link /><![CDATA[http://banhada.kr/?cateCode=09&viewCode=S0941580]]>
<time_start>2011-10-10 09:00:00</time_start>
<time_end>2011-10-17 09:00:00</time_end>
<price_original>35000</price_original>
<price_now>20000</price_now>
</item>

As you can see above, tag is a little strange. In my opinion, that( tag) is not a stand XML form, right? How can I parse this terrible form?

如上所示,标签有点奇怪。在我看来,(标签)不是一个立场XML形式,对吗?我该如何解析这种可怕的形式?

2 个解决方案

#1


7  

You don't need BeautifulStoneSoup or lxml. Python's included batteries do the job just fine, and there doesn't seem to be anything non-compliant about your XML.

你不需要BeautifulStoneSoup或lxml。包含Python的电池可以很好地完成工作,并且似乎没有任何与XML不兼容的东西。

>>> content='''\
... <item>
... <title><![CDATA[Title Sample]]></title>
... <link /><![CDATA[http://banhada.kr/?cateCode=09&viewCode=S0941580]]>
... <time_start>2011-10-10 09:00:00</time_start>
... <time_end>2011-10-17 09:00:00</time_end>
... <price_original>35000</price_original>
... <price_now>20000</price_now>
... </item>'''
>>> import xml.etree.cElementTree as et
>>> foo = et.XML(content)
>>> for e in foo:
...     print e.tag, e.text, repr(e.tail)
...
title Title Sample '\n'
link None 'http://banhada.kr/?cateCode=09&viewCode=S0941580\n'
time_start 2011-10-10 09:00:00 '\n'
time_end 2011-10-17 09:00:00 '\n'
price_original 35000 '\n'
price_now 20000 '\n'
>>>

#2


7  

You could use BeautifulSoup to parse XML:

您可以使用BeautifulSoup来解析XML:

import bs4 as bs
content='''\
<item>
<title><![CDATA[Title Sample]]></title>
<link /><![CDATA[http://banhada.kr/?cateCode=09&viewCode=S0941580]]>
<time_start>2011-10-10 09:00:00</time_start>
<time_end>2011-10-17 09:00:00</time_end>
<price_original>35000</price_original>
<price_now>20000</price_now>
</item>'''    

soup = bs.BeautifulSoup(content, 'xml')

title = soup.title
print(title.string)
# Title Sample

link = soup.link.nextSibling
print(link)
# http://banhada.kr/?cateCode=09&viewCode=S0941580

Under the hood, BeautifulSoup uses lxml for parsing XML. Although it's not needed here, you might want to use lxml directly, since it gives you more succinct ways to navigate through XML using XPath:

在引擎盖下,BeautifulSoup使用lxml来解析XML。虽然这里不需要它,但您可能希望直接使用lxml,因为它为您提供了使用XPath在XML中导航的更简洁方法:

import lxml.etree as ET

content='''\
<item>
<title><![CDATA[Title Sample]]></title>
<link /><![CDATA[http://banhada.kr/?cateCode=09&viewCode=S0941580]]>
<time_start>2011-10-10 09:00:00</time_start>
<time_end>2011-10-17 09:00:00</time_end>
<price_original>35000</price_original>
<price_now>20000</price_now>
</item>'''    

doc = ET.fromstring(content)

title = doc.find('title')
print(title.text)
# Title Sample

link = doc.find('link')
print(link.tail)
# http://banhada.kr/?cateCode=09&viewCode=S0941580

#1


7  

You don't need BeautifulStoneSoup or lxml. Python's included batteries do the job just fine, and there doesn't seem to be anything non-compliant about your XML.

你不需要BeautifulStoneSoup或lxml。包含Python的电池可以很好地完成工作,并且似乎没有任何与XML不兼容的东西。

>>> content='''\
... <item>
... <title><![CDATA[Title Sample]]></title>
... <link /><![CDATA[http://banhada.kr/?cateCode=09&viewCode=S0941580]]>
... <time_start>2011-10-10 09:00:00</time_start>
... <time_end>2011-10-17 09:00:00</time_end>
... <price_original>35000</price_original>
... <price_now>20000</price_now>
... </item>'''
>>> import xml.etree.cElementTree as et
>>> foo = et.XML(content)
>>> for e in foo:
...     print e.tag, e.text, repr(e.tail)
...
title Title Sample '\n'
link None 'http://banhada.kr/?cateCode=09&viewCode=S0941580\n'
time_start 2011-10-10 09:00:00 '\n'
time_end 2011-10-17 09:00:00 '\n'
price_original 35000 '\n'
price_now 20000 '\n'
>>>

#2


7  

You could use BeautifulSoup to parse XML:

您可以使用BeautifulSoup来解析XML:

import bs4 as bs
content='''\
<item>
<title><![CDATA[Title Sample]]></title>
<link /><![CDATA[http://banhada.kr/?cateCode=09&viewCode=S0941580]]>
<time_start>2011-10-10 09:00:00</time_start>
<time_end>2011-10-17 09:00:00</time_end>
<price_original>35000</price_original>
<price_now>20000</price_now>
</item>'''    

soup = bs.BeautifulSoup(content, 'xml')

title = soup.title
print(title.string)
# Title Sample

link = soup.link.nextSibling
print(link)
# http://banhada.kr/?cateCode=09&viewCode=S0941580

Under the hood, BeautifulSoup uses lxml for parsing XML. Although it's not needed here, you might want to use lxml directly, since it gives you more succinct ways to navigate through XML using XPath:

在引擎盖下,BeautifulSoup使用lxml来解析XML。虽然这里不需要它,但您可能希望直接使用lxml,因为它为您提供了使用XPath在XML中导航的更简洁方法:

import lxml.etree as ET

content='''\
<item>
<title><![CDATA[Title Sample]]></title>
<link /><![CDATA[http://banhada.kr/?cateCode=09&viewCode=S0941580]]>
<time_start>2011-10-10 09:00:00</time_start>
<time_end>2011-10-17 09:00:00</time_end>
<price_original>35000</price_original>
<price_now>20000</price_now>
</item>'''    

doc = ET.fromstring(content)

title = doc.find('title')
print(title.text)
# Title Sample

link = doc.find('link')
print(link.tail)
# http://banhada.kr/?cateCode=09&viewCode=S0941580