加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – OSError:[Errno 36]文件名太长:

发布时间:2020-12-20 11:42:34 所属栏目:Python 来源:网络整理
导读:我需要将网页转换为 XML(使用 Python 3.4.3).如果我将URL的内容写入文件,那么我可以完美地阅读和解析它,但如果我尝试直接从网页上读取,我的终端中会出现以下错误: File “./AnimeXML.py”,line 22,in xml = ElementTree.parse (xmlData) File “/Library/Fr
我需要将网页转换为 XML(使用 Python 3.4.3).如果我将URL的内容写入文件,那么我可以完美地阅读和解析它,但如果我尝试直接从网页上读取,我的终端中会出现以下错误:

File “./AnimeXML.py”,line 22,in
xml = ElementTree.parse (xmlData)
File “/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/xml/etree/ElementTree.py”,line 1187,in parse
tree.parse(source,parser)
File “/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/xml/etree/ElementTree.py”,line 587,in parse
source = open(source,“rb”)
OSError: [Errno 36] File name too long:

我的python代码:

# AnimeXML.py
#! /usr/bin/Python

# Import xml parser.
import xml.etree.ElementTree as ElementTree

# XML to parse.
sampleUrl = "http://cdn.animenewsnetwork.com/encyclopedia/api.xml?anime=16989"

# Read the xml as a file.
content = urlopen (sampleUrl)

# XML content is stored here to start working on it.
xmlData = content.readall().decode('utf-8')

# Close the file.
content.close()

# Start parsing XML.
xml = ElementTree.parse (xmlData)

# Get root of the XML file.
root = xml.getroot()

for info in root.iter("info"):
    print (info.attrib)

有什么方法可以修复我的代码,以便我可以直接将网页读入python而不会出现此错误吗?

解决方法

正如ElementTree文档的 Parsing XML部分所述:

We can import this data by reading from a file:

import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()

Or directly from a string:

root = ET.fromstring(country_data_as_string)

您将整个XML内容作为巨型路径名传递.您的XML文件可能大于2K,或者您的平台的最大路径名大小,因此错误.如果不是,那么你只会得到一个不同的错误,即没有名为[所有内容都在你的XML文件中的第一个/所有内容].

只需使用fromstring而不是解析.

或者,请注意parse可以获取文件对象,而不仅仅是文件名.而urlopen返回的东西是一个文件对象.

另请注意该部分的下一行:

fromstring() parses XML from a string directly into an Element,which is the root element of the parsed tree. Other parsing functions may create an ElementTree.

所以,你不希望root = tree.getroot().

所以:

# ...
content.close()
root = ElementTree.fromstring(xmlData)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读