My first post :o) Also my first python steps. I want to fetch data from an xml api and searched for some youtube videos. I found this one: https://youtu.be/jeZp5NE9lII?t=685 (XML part)
I "copied" the code from the video (where the python script runs) to found out if I can fetch informations from an XML API or if I need to add something to my ATOM or Python. Here the code:
import requests
import xml.etree.ElementTree as ET
url = 'http://api.worldbank.org/countries'
response = requests.get(url)
country_data = response.text
countries = ET.fromstring(country_data)
namespaces = {'wb': 'http://www.worldbank.org'}
for country in countries.findall('wb:country', namespaces):
name = country.find('wb:name', namespaces).text
code = country.find('wb:iso2Code', namespaces).text
print('country: {} {}'.format(name, code))
Had some errors. So I installed on my mac:
sudo easy_install pippython -m pip install requests
Now I have the following error:
File "/var/folders/2k/ynjbnhl17pg6_wtr20l149xh0000gn/T/atom_script_tempfiles/3a3bf940-16b6-11eb-8d53-6b13b4204efb", line 30, in <module>
countries = ET.fromstring(country_data)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 1311, in XML
parser.feed(text)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 1657, in feed
self._parser.Parse(data, 0)
UnicodeEncodeError: 'ascii' codec can't encode```
What I did wrong?
pip listto find out what installs you need to do in Python 3 to replicate what you have in Python 2.