当XML标记名称包含大写字母BeautifulSoup提高AttributeError的

编程入门 行业动态 更新时间:2024-10-24 08:29:38
本文介绍了当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="upload.wikimedia/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的

本文发布于:2023-11-12 12:41:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581520.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:大写字母   标记   名称   XML   BeautifulSoup

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!