Python Forum

Full Version: AttributeError: 'NoneType' object has no attribute error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am writing a python script for scrapping weather of different city from weather-forecast website. I got an AttributeError in my code and i am unable to gets the cause of error.

Here is my code:

import urllib.request, urllib.parse, urllib.error
import ssl
import re

# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

condition = 1
braces = '<'

    end = 0
    start = 0
    city = input('enter city: ')
    url = "" + city + "/forecasts/latest"
    rawData = urllib.request.urlopen(url, context=ctx).read()
    data = rawData.decode()
    string ='1 &ndash; 3 Day Weather Forecast Summary:</b><span class="read-more-small"><span class="read-more-content"><span class="phrase">', data)
    start = string.end()

    i = 0
    for i in range(start,start+500):
        if data[i] == braces:
            end = i

    weather = data[start:end]
    final = weather.replace("&deg;C"," °C")

This is an error occured:
enter city: Mumbai Traceback (most recent call last): File "C:\Users\VISHAL\Desktop\wf\", line 21, in <module> start = string.end() AttributeError: 'NoneType' object has no attribute 'end'
the reason for this particular error is that your regex does not match anything and returns None. Note that using Regex to parse HTML is not working, you need package like BeautifulSoup. However this site is using javascript to produce the page you see (and that is one more reason why your regex does not match anything), so at the end you will need to use tools like selenium if you want to scrape it.