Search code examples
pythonweb-scrapingattributeerror

Condition for skipping web page where data is not available


I'm trying to fetch data from 'http://weather.uwyo.edu/upperair/sounding.html' website. I wrote following script but the problem is some stations in given site are not having any data. So it's giving error as 'AttributeError: 'NoneType' object has no attribute 'text''. I want to write some condition so that wherever data is not there it should skip that station and go for next station.

try: for stn in station:

    year = '2017'
    month = '08'
    day = '14'
    hour = '00'
    end = '12'

url = requests.get('http://weather.uwyo.edu/cgi-bin/sounding?region=naconf&TYPE=TEXT%3ALIST&YEAR='+year+'&MONTH='+month+'&FROM='+day+hour+'&TO='+day+end+'&STNM='+str(stn))

webbrowser.open('http://weather.uwyo.edu/cgi-bin/sounding?region=seasia&TYPE=TEXT%3ALIST&YEAR='+year+'&MONTH='+month+'&FROM='+day+hour+'&TO='+day+end+'&STNM='+str(stn))

soup = BeautifulSoup(url.text,'html.parser')
data_box = soup.find('pre')
data = data_box.text.strip()
print (data)

except AttributeError : print("No data available for station", AttributeError)

import sys
import webbrowser
import urllib3
import requests
import lxml.html as lh
import pandas as pd
from time import sleep
from bs4 import BeautifulSoup
import csv

station =[42647,42101]       # [42101] #,42647,42971,43371]

try:
    for stn in station:

        year = '2017'
        month = '08'
        day = '14'
        hour = '00'
        end = '12'

    url = requests.get('http://weather.uwyo.edu/cgi-bin/sounding?region=naconf&TYPE=TEXT%3ALIST&YEAR='+year+'&MONTH='+month+'&FROM='+day+hour+'&TO='+day+end+'&STNM='+str(stn))

webbrowser.open('http://weather.uwyo.edu/cgi-bin/sounding?region=seasia&TYPE=TEXT%3ALIST&YEAR='+year+'&MONTH='+month+'&FROM='+day+hour+'&TO='+day+end+'&STNM='+str(stn))

  soup = BeautifulSoup(url.text,'html.parser')
  data_box = soup.find('pre')
  data = data_box.text.strip()
 except AttributeError :
        print("No data available for  station", AttributeError)
    print (data)

I expect it should print data for data available stations by skipping stations where data is not available.But the output is : No data available for station Traceback (most recent call last): File "sound.py", line 30, in data = data_box.text.strip() AttributeError: 'NoneType' object has no attribute 'text'


Solution

  • You have the exception handling scoping wrong; the exception is bubbled up and catched outside for loop, so the for loop already exited at that point.

    You need to set the try-except where you think the exception could be raised inside the for loop i.e. choose the smallest scope:

    for stn in station:
        ...
        try:
            data = data_box.text.strip()
         except AttributeError :
            print("No data available for  station", AttributeError)
            continue  # move onto next station