【问题标题】:how to get content inside CDATA of XML tag django如何在 XML 标签 django 的 CDATA 中获取内容
【发布时间】:2013-11-08 02:39:02
【问题描述】:

我正在使用 urllib 和 beautifulsoup 来解析 django 中的 xml 文件。 我无法使用 CDATA 解析描述标签的内容。

我的 xml 标签。

<item>
         <title>EU Confronting US Over Surveillance</title>
    <description><![CDATA[Voice of America is an international news and broadcast organization serving Central and Eastern Europe, the Caucasus, Central Asia, Russia, the Middle East and Balkan countries]]></description>
<guid>http://www.voanews.com/content/eu-confronting-us-over-surveillance/1778928.html</guid>
</item>

这个描述标签在项目标签里面 视图.py

for i in soup.findAll('item'):
 print i.description.string

如果 CDATA 不存在意味着我可以解析描述标签内的内容。我不知道如何解析这个内容。 请帮帮我 还有如何获取标签内的图像..

<description>&lt;img src='http://static.ibnlive.in.com/ibnlive/pix/sitepix/10_2013/tony-abbott-visits-afghanistan-says-australias-war-is-over_291013013344_338x225.jpg' width='90' height='62'&gt;&lt;p&gt;"Australia's longest war" is ending and its defence forces mission in Afghanistan will be complete by 2013 end, Prime Minister Tony Abbott announced in a statement on Tuesday.&lt;/p&gt;</description>

【问题讨论】:

标签: python xml django


【解决方案1】:

CData 可以这样访问:

>>> import BeautifulSoup
>>> txt = '''<description><![CDATA[Voice of America is an international news and broadcast organization serving Central and Eastern Europe, the Caucasus, Central Asia, Russia, the Middle East and Balkan countries]]></description>'''
>>> soup = BeautifulSoup.BeautifulSoup(txt)
>>> for cd in soup.findAll(text=True):
...   if isinstance(cd, BeautifulSoup.CData):
...     print 'CData value: %r' % cd
...
CData value: u'Voice of America is an international news and broadcast organi
zation serving Central and Eastern Europe, the Caucasus, Central Asia, Russia, t
he Middle East and Balkan countries'
>>>

根据您的评论进行的编辑应该会有所帮助。

from bs4 import BeautifulSoup, CData
import urllib

source_txt = urllib.urlopen("http://voanews.com/api/epiqq")
soup = BeautifulSoup.BeautifulSoup(source_txt.read())
for cd in soup.findAll(text=True):
    if isinstance(cd, CData):
        print 'CData value: %r' % cd        

注意事项:

  • 导入语句。我正在导入整个 BeautifulSoup 包
  • urlopen 参数。它需要http

【讨论】:

  • 正在解析新闻提要。例如,我使用了这个网址“voanews.com/api/epiqq”。在您的第 3 行代码中,它显示以下错误 AttributeError: type object 'BeautifulSoup' has no attribute 'read'。我将整个 url 削减为 sorce_txt=urllib .urlopen("voanews.c/api/epiqq") b=BeautifulSoup(sorce_txt.read())..... 不是特定的 xml 标签。我想解析整个 url,即每一个描述标记在那个...给我如何做的建议安迪
  • 是的,您的代码是正确的,安迪...我需要如何为整个网址执行此操作。
  • 我正在尝试告诉你,如果我有任何错误...请当时指导我安迪
  • >>> 导入 urllib >>> source_txt=urllib.urlopen("voanews.com/api/epiqq") >>> 从 bs4 导入 BeautifulSoup >>> b=BeautifulSoup(source_txt.read()) >>> for i in b.findAll('item'): ... for j in i.findAll('description'): ... for k in j.findAll(text=True): ... print i I didn'什么都没有..什么都没有打印出来..请帮帮我
  • 是的,安迪你是对的......如果它是一个单一的标签意味着我们可以在字符串中定义它并像你已经说过的那样解析......正在解析一个网址。这个 url 的 xml 文件包含更多这样的标签,所以我不能把它作为字符串给出。在那种情况下,我该如何解析?会检查并告诉你
猜你喜欢
  • 1970-01-01
  • 2011-01-17
  • 2012-10-23
  • 1970-01-01
  • 1970-01-01
  • 2015-07-02
  • 2016-02-05
  • 1970-01-01
  • 2012-05-15
相关资源
最近更新 更多