supercoderz / pyflightdata

A simple library to get flight data from various websites from within Python code
MIT License
78 stars 28 forks source link

AttributeError: 'NoneType' object has no attribute 'find' #19

Closed E3V3A closed 5 years ago

E3V3A commented 5 years ago

I just reinstalled arrivals on python3.8 and with latest updates of lxml etc.

However, I'm running into to the following error (also from command line):

# python3 -c "from pyflightdata import FlightData as fd;da=fd().get_airports('India');print(da);"

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/cygdrive/c/cygwin64/lib/python3.8/site-packages/pyflightdata/flightdata.py", line 135, in get_airports
    return self._fr24.get_airports_data(url)
  File "/cygdrive/c/cygwin64/lib/python3.8/site-packages/pyflightdata/common_fr24.py", line 181, in get_airports_data
    data = self.get_raw_airport_data(url)
  File "/cygdrive/c/cygwin64/lib/python3.8/site-packages/pyflightdata/common_fr24.py", line 157, in get_raw_airport_data
    return self.get_raw_data(url, 'tbl-datatable', 'tbody', 'tr') or []
  File "/cygdrive/c/cygwin64/lib/python3.8/site-packages/pyflightdata/common.py", line 112, in get_raw_data
    return self.traverse(soup, elemid, elements)
  File "/cygdrive/c/cygwin64/lib/python3.8/site-packages/pyflightdata/common.py", line 103, in traverse
    res = res.find(element)
AttributeError: 'NoneType' object has no attribute 'find'

It's origin seem to be bautifulsoup4 when using the find() in common.py:

    def traverse(self, soup, key, elements, by_class=False):
        res = soup.find(attrs={'id': key}) if not by_class else soup.find(
            attrs={'class': key})
        if elements:
            for element in elements[:-1]:
                res = res.find(element)
            return res.find_all(elements[-1])
return res

Stackoverflow describe these types of issues here:

E3V3A commented 5 years ago

Further look seem to point to the FR24 html may have changed a little...

Here looking at Airports in India:


firefox_2019-10-15_18-58-23

E3V3A commented 5 years ago

@hari-allamraju Can you please have a look at this? (I'm using this feature at least a few times a week.)

hari-allamraju commented 5 years ago

Working on this now - will keep you posted

hari-allamraju commented 5 years ago

The error is a bit more weird - lxml cannot find the data table by key tbl-datatable anymore - I see it in the content from requests but not in the output of beautifulsoup

hari-allamraju commented 5 years ago

@E3V3A - do you want to try the master branch code? I moved to using html5 lib parser and it seems to work now; also reworked traverse method to handle multiple occurrences of the tags Looking to add more tests if time permits. If you can test the master branch then I will cut a build to pypi

E3V3A commented 5 years ago

@hari-allamraju Fantastic! Now it works again.