Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I've been trying to parse a website for a while but I keep getting errors while trying to do so. When I try to use XML I'm getting this:

>>>xml.etree.ElementTree.ParseError: no element found: line 1, column

When I try to use json I'm getting this:

>>>json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

What I have tried:

#My code for xml is:

Python
    import urllib.request, urllib.parse, urllib.error
    import ssl
    import xml.etree.ElementTree as PT
    
    ctx = ssl.create_default_context()
    ctx.check_hostname = False
    ctx.verify_mode = ssl.CERT_NONE
    
    while True:
    
     url= input('Enter Location: ')
     print('Retrieving', url)
    
     uh = urllib.request.urlopen(url, context=ctx)
     data = uh.read()
     print('Retrieved', len(data), 'characters')
    
     tree=PT.fromstring (uh.read())
    
     print (tree)
     break

#My code for json is:

    <pre lang="Python">
    import urllib.request, urllib.parse, urllib.error
    import ssl
    import json
    import requests
    
    ctx = ssl.create_default_context()
    ctx.check_hostname = False
    ctx.verify_mode = ssl.CERT_NONE
    
    while True:
    
     url= input('Enter Location: ')
     print('Retrieving', url)
    
     r = requests.get(url, auth=('user', 'pass'))
    
     m=r.json()
    
     print (m)
     break
Posted
Comments
Richard MacCutchan 10-Jul-21 6:34am    
Both messages suggest that the data you have captured is not in a valid format. So you need to examine exactly what text your code is dealing with.
Sanjana “Sandyy” 10-Jul-21 8:56am    
Is there a specific way to do that? Just a pointer would be helpful.
Richard MacCutchan 11-Jul-21 4:03am    
A specific way to do what? You are trying to parse some data that is not in the correct format. So look at the data to find out why.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900