无法搜索搜索后找到的标签

时间:2015-09-24 17:24:18

标签: python parsing python-3.x beautifulsoup bs4

我对BeautifulSoup还有其他问题。这是我的代码:

def parse_airfields():
    html = urlopen('https://www.sia.aviation-civile.gouv.fr/aip/enligne/FRANCE/AIRAC-2015-09-17/html/eAIP/FR-AD-1.3-fr-FR.html').read()
    soup = BeautifulSoup(html, 'lxml')

    # delete <del></del>
    for d in soup.find_all('del'):
        d.decompose()

    airfields = []
    current_airfield = None

    for tr in soup.find('tbody').find_all('tr'):
        if tr.td.span.text.isalpha():  # airfield
            if current_airfield != None:  # dont apend an empty airfield on the first loop
                airfields.append(current_airfield)

            # parse new airfield
            data = [''.join(td.stripped_strings) for td in tr.find_all('td')]
            current_airfield = dict(zip(labels_airfield, data))
            current_airfield['runways'] = []
        else:  # runway
            data = [''.join(td.stripped_strings) for td in tr.find_all('td')]
            current_runway = dict(zip(labels_runway, data))

            current_airfield['runways'].append(current_runway)

    # append last airfield
    airfields.append(current_airfield)

    return airfields

但是当我运行它时,我有一个错误说我:

Traceback (most recent call last):
  File "algo.py", line 85, in <module>
    airfields = parse_airfields()
  File "algo.py", line 69, in parse_airfields
    data = [''.join(td.stripped_strings) for td in tr.find_all('td')]
  File "/home/louis/.local/lib/python3.4/site-packages/bs4/element.py", line 1255, in find_all
    return self._find_all(name, attrs, text, limit, generator, **kwargs)
  File "/home/louis/.local/lib/python3.4/site-packages/bs4/element.py", line 525, in _find_all
    return ResultSet(strainer, result)
  File "/home/louis/.local/lib/python3.4/site-packages/bs4/element.py", line 1712, in __init__
    super(ResultSet, self).__init__(result)
  File "/home/louis/.local/lib/python3.4/site-packages/bs4/element.py", line 522, in <genexpr>
    result = (element for element in generator
  File "/home/louis/.local/lib/python3.4/site-packages/bs4/element.py", line 1273, in descendants
    current = current.next_element
AttributeError: 'NoneType' object has no attribute 'next_element'

我的BeautifulSoup版本是4.4.0,但它适用于拥有4.3.2的朋友计算机。

这些版本之间有什么区别?如何在我的计算机上运行代码?

感谢您的帮助!

0 个答案:

没有答案