pythonjsonapiweb-scrapingurlopen

JSONDecodeError: Expecting value: line 1 column 1 (char 0), I am getting this error


How to fix this error JSONDecodeError: Expecting value: line 1 column 1 (char 0)? Below is the code

    from urllib.request import urlopen
    api_url = "https://samples.openweathermap.org/data/2.5/weatherq=London&mode=html&appid=b6907d289e10d714a6e88b30761fae22"
url_result = urlopen(api_url)
data = url_result.read()
data=data.decode('utf-8')
import json
json_data = json.loads(data)

Solution

  • You are making a request with mode=html then attempting to parse the response as JSON. You can dynamically pick up the correct urls by first getting a list of the available samples (and modes) from the base domain. The filter for what you want. JSON is the default to doesn't need the mode specified. The advantage here is you dynamically pick up the appid.

    I show html and JSON responses below.


    import requests
    from bs4 import BeautifulSoup as bs
    
    with requests.Session() as s:
        r = s.get('https://samples.openweathermap.org/').json()
        samples = r['products']['current_weather']['samples'] 
        url_html = [sample for sample in samples if 'London&mode=html' in sample][0]
        print(url_html)
        print()
        r = s.get(url_html)
        soup = bs(r.content, 'lxml')
        print(soup.get_text())
        print()
        url_json = [sample for sample in samples if 'London&appid' in sample][0]
        print(url_json)
        print()
        r = s.get(url_json).json()
        print(r)