Jsondecodeerror Expecting value: line 1 column 1 (char 0) - content-type: text/xml

Asked

Viewed 73 times

1

I have a project to capture Atms next to a coordinate on the Mastercard website form. I can bring the result but not in json. By Content-Type be text.XML, should not allow to bring result in json? Or result will always be HTML and need to apply Beautifulsoap in response?

Source https://www.mastercard.pt/pt-pt/consumers/get-support/locate-an-atm.html

import requests 
params = {'latitude': -23.4887194, 'longitude': -46.6701079, 'radius': 5, 'distanceUnit': 1,
'locationType': 'atm', 'maxLocations': "", 'instName': "", 'supportEMV': "", 'customAttr1': "",
'locationTypeId': ""}
r = requests.get('https://www.mastercard.pt/locator/NearestLocationsService/', params=params)
r.json()

ERROR

ssss    ---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
<ipython-input-41-52b7ec8d97cc> in <module>
----> 1 r.json()
~\Anaconda3\lib\site-packages\requests\models.py in json(self, **kwargs)
896                     # used.
897                     pass
898         return complexjson.loads(self.text, **kwargs)
899 
900     @property
~\Anaconda3\lib\json\__init__.py in loads(s, cls, object_hook, parse_float, parse_int, 
parse_constant, object_pairs_hook, **kw)
K 355             parse_int is None and parse_float is None and
356             parse_constant is None and object_pairs_hook is None and not kw):
357         return _default_decoder.decode(s)
358     if cls is None:
359         cls = JSONDecoder
~\Anaconda3\lib\json\decoder.py in decode(self, s, _w)
335 
336        
337         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
338         end = _w(s, end).end()
339         if end != len(s):
~\Anaconda3\lib\json\decoder.py in raw_decode(self, s, idx)
353             obj, end = self.scan_once(s, idx)
354         except StopIteration as err:
355             raise JSONDecodeError("Expecting value", s, err.value) from None
356         return obj, end
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
  • xml is another format to carry data, it is very similar to json. You will need a lib of your own to handle xml. I will search here

  • Why don’t you use the bilioteca they provide??? See here

1 answer

1

Hello, I don’t know exactly what you want to do but can read the xml this way:

import requests
import xml.etree.ElementTree as ET


params = {
          'latitude': -23.4887194,
          'longitude': -46.6701079,
          'radius': 5,
          'distanceUnit': 1,
          'locationType': 'atm',
          'maxLocations': "",
          'instName': "",
          'supportEMV': "",
          'customAttr1': "",
          'locationTypeId': ""
        }

url = 'https://www.mastercard.pt/locator/NearestLocationsService/'
r = requests.get(url, params=params)
tree = ET.fromstring(r.content)

# uso asterisco aqui para pegar todos os elementos
# mas você pode pegar um especifico se souber o nome.
# ... tree.iter(nome_do_elemento) <- assim
for child in tree.iter("*"):
    print(child.tag, child.attrib)

To requests by default does not manipulate xml, hence the error. xml is an old format :/

elementtree documentation

  • I didn’t know this library. Very convenient it. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.