Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
builtins.TypeError:无效文件:正在读取xml文件-python_Python_Xml - Fatal编程技术网

builtins.TypeError:无效文件:正在读取xml文件-python

builtins.TypeError:无效文件:正在读取xml文件-python,python,xml,Python,Xml,看起来这个方法不起作用,它返回我内置的。类型错误:无效文件:尽管内容类型返回xml数据。有什么办法解决这个问题吗?试着换一行: import xml.etree.ElementTree as reader import requests web_data = 'http://api.worldbank.org/incomeLevels/LIC/countries' a = requests.get(web_data) print(a.headers['Content-Type']) print

看起来这个方法不起作用,它返回我内置的。类型错误:无效文件:尽管内容类型返回xml数据。有什么办法解决这个问题吗?

试着换一行:

import xml.etree.ElementTree as reader
import requests

web_data = 'http://api.worldbank.org/incomeLevels/LIC/countries'
a = requests.get(web_data)
print(a.headers['Content-Type'])
print(reader.parse(a).getroot())

a是响应对象,a.text是内容

另外,reader.parse需要一个文件名,如果要从字符串中解析,则必须使用该方法

请注意,fromstring方法已经返回xml的根元素

print(reader.parse(a).getroot())
print(reader.fromstring(a.text))