Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 AttributeError: 'NoneType' object has no attribute error
#1
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 = '<'

while(condition):
    end = 0
    start = 0
    city = input('enter city: ')
    url = "http://www.weather-forecast.com/locations/" + city + "/forecasts/latest"
    rawData = urllib.request.urlopen(url, context=ctx).read()
    data = rawData.decode()
    string = re.search('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
            break

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

This is an error occured:
Error:
enter city: Mumbai Traceback (most recent call last): File "C:\Users\VISHAL\Desktop\wf\weather.py", line 21, in <module> start = string.end() AttributeError: 'NoneType' object has no attribute 'end'
Quote
#2
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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  AttributeError: 'str' object has no attribute 'xpath' nazmulfinance 4 207 Nov-11-2019, 05:15 PM
Last Post: nazmulfinance
  AttributeError: 'str' object has no attribute 'xpath' nazmulfinance 0 132 Nov-10-2019, 09:13 PM
Last Post: nazmulfinance
  form.populate_obj problem "object has no attribute translate" pascale 0 325 Jun-12-2019, 07:30 PM
Last Post: pascale
  Error object has no attribute text hcyeap 3 4,709 May-21-2019, 07:12 AM
Last Post: buran
  AttributeError: 'Response' object has no attribute 'replace' Truman 12 4,497 Mar-20-2019, 12:59 AM
Last Post: ichabod801
  Error: module 'urllib' has no attribute 'urlopen' mitmit293 2 2,395 Jan-29-2019, 02:32 PM
Last Post: snippsat
  AttributeError: 'dict' object has no attribute 'is_active' (PyMongo And Flask) usman 0 1,047 Nov-20-2018, 09:50 PM
Last Post: usman
  500 error cannot deserialize json object SaiBhargav 0 667 Sep-18-2018, 09:53 PM
Last Post: SaiBhargav
  BeautifulSoup 'NoneType' object has no attribute 'text' bmccollum 9 4,570 Sep-14-2018, 12:56 PM
Last Post: bmccollum
  'ellipsis' object has no attribute 'lower' Bharath_AK 4 2,000 Mar-15-2018, 07:15 AM
Last Post: Bharath_AK

Forum Jump:


Users browsing this thread: 1 Guest(s)