2017-04-13 5 views
0
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()) 

それは、このメソッドは動作しません見えます、それは私を返しbuiltins.TypeError:無効なファイル:「コンテンツタイプを返すにもかかわらずをxmlデータこれを解決する方法任意のアイデアbuiltins.TypeError:無効なファイル:<応答[200]>フェイント読み取りxmlファイル - パイソン

答えて

1

ラインを変更してみてください:。?

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

aは

print(reader.fromstring(a.text)) 

へ応答オブジェクト、a.textはコンテンツです。

reader.parse()fromstringメソッドを使用する必要がある文字列から解析したい場合は、ファイル名が必要です。

fromstringメソッドは、すでにxmlのルート要素を返していることに注意してください。

+0

はい、動作します。ありがとうございました。 –

関連する問題