当XML标记名称包含大写字母BeautifulSoup提高AttributeError的 [英] BeautifulSoup raise AttributeError when xml tag name contains capital letters

查看:396
本文介绍了当XML标记名称包含大写字母BeautifulSoup提高AttributeError的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让所有的XML属性标记名称

I'm trying to get all the XML attributes for the tag Name.

收到此错误:

AttributeError: 'NoneType' object has no attribute 'attrs'

当我执行以下code:

when I executed the following code:

import BeautifulSoup as bs

xml = '''
<Product Code="1" HighPic="http://upload.wikimedia.org/wikipedia/commons/thumb/5/5f/Linksys48portswitch.jpg/220px-Linksys48portswitch.jpg" HighPicHeight="320" HighPicSize="37217" HighPicWidth="400" ID="35" Title="Demo Product">
<Category ID="23">
<Name ID="57" Value="Switches" langid="1"/>
</Category>
</Product>'''

doc = bs.BeautifulSoup(xml)
div = doc.find("Name")

for attr, val in div.attrs:
    print "%s:%s" % (attr, val)

我改变了标签名称,然后它的作品。

为什么会出现这个错误时,变量名称包含大写字母?

Why am I getting this error when the tag name contains capital letters?

推荐答案

BeautifulSoup是一个HTML的解析库,主要是。它可以处理XML太多,但所有标签小写按照HTML规范。引述 BeautifulSoup文档

BeautifulSoup is a HTML-parsing library, primarily. It can handle XML too, but all tags are lowercased as per the HTML specification. Quoting the BeautifulSoup documentation:

由于HTML标记和属性是不区分大小写,所有三个HTML解析器转换标签和属性的名称为小写。也就是说,标记&LT; TAG&GT;&LT; / TAG&GT; 转换为&LT;标签&GT;&LT; /标签&GT; 。如果您想preserve大小写混合的或大写的标记和属性,则需要解析文档为XML。

Because HTML tags and attributes are case-insensitive, all three HTML parsers convert tag and attribute names to lowercase. That is, the markup <TAG></TAG> is converted to <tag></tag>. If you want to preserve mixed-case or uppercase tags and attributes, you’ll need to parse the document as XML.

的一个 XML作案其中标签是匹配大小写敏感,不小写,但是这需要安装在 LXML 库。因为 LXML 是C扩展库,这是不支持谷歌应用程序引擎。

There is a XML modus where tags are matches case-sensitively and are not lowercased, but this requires the lxml library to be installed. Because lxml is a C-extension library, this is not supported on the Google App Engine.

使用 ElementTree的API,而不是

import xml.etree.ElementTree as ET

root = ET.fromstring(xml)
div = root.find('.//Name')

for attr, val in div.items():
     print "%s:%s" % (attr, val)

这篇关于当XML标记名称包含大写字母BeautifulSoup提高AttributeError的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆