Объект «ResultSet» не имеет атрибута «findAll» сообщение об ошибке в Beautiful Soup внутри функции

Я изучаю Pyhton и, в частности, красивый суп, и я выполняю упражнение Google по регулярному выражению, используя набор html-файлов, содержащих популярные имена детей разных лет (например, baby1990.html и т. д.). Если вам интересно, вы можете найти этот набор данных здесь: https://developers.google.com/edu/python/exercises/baby-names

Каждый html-файл содержит таблицу с данными об именах детей, которая выглядит следующим образом:

введите здесь описание изображения

Я составил функцию, которая извлекает имена детей из файлов html и сохраняет их в фреймах данных, фреймах данных в словаре и всех фреймах данных, объединенных в один фрейм данных.

В каждом html-файле есть две таблицы. Таблица, содержащая данные о ребенке, имеет следующий HTML-код:

<table width="100%" border="0" cellspacing="0" cellpadding="4" summary="formatting">

В этой строке отличительным атрибутом является аннотация = "форматирование".

Функция, которую я написал, была отредактирована на основе полученных отзывов и выглядит следующим образом:

def babynames(path):

# This function takes the path of the directory where the html files are stored and returns a list containing the 
# a dataframe which encompasses all the tabular baby-names data in the files and as well as a dictionary holding
# a separate dataframe for each html file

# 0: Initialize objects
dicnames = {}  # will hold the dataframes containing the tabular data of each year
dfnames = pd.DataFrame([])  # will hold the aggregate data

# 1: Create a list containing the full paths of the baby files in the directory indicated by the path argument of the babynames
# function
allfiles = files(path)

# 2: Begin the looping through the files 

for file in allfiles: 
        with open(file,"r") as f: soup = bs(f.read(), 'lxml')  # Convert the file to a soup

        # 3. Initialize empty lists to hold the contents of the cells
        Rank=[]
        Baby_1 =[]
        Baby_2 =[] 
        df = pd.DataFrame([])

        # 4. Extract the Table containing the Baby data and loop through the rows of this table

        for row in soup.select("table[summary=formatting] tr"):

         # 5. Extract the cells 

            cells = row.findAll("td")

            # 6. Convert to text and append to lists
            try:
                Rank.append(cells[0].find(text=True))  
                Baby_1.append(cells[1].find(text=True))
                Baby_2.append(cells[2].find(text=True))
            except:
                print "file: " , file
                try:
                        print "cells[0]: " , cells[0]
                except:
                        print "cells[0] : NaN"
                try:
                        print "cells[1]: " , cells[1]
                except:
                        print "cells[1] : NaN"    
                try:
                        print "cells[2]: " , cells[2]
                except:
                        print "cells[2] : NaN"   

            # 7. Append the lists to the empty dataframe df
            df["Rank"] = Rank 
            df["Baby_1"] = Baby_1
            df["Baby_2"] = Baby_2

            # 8. Append the year to the dataframe as a separate column
            df["Year"] = extractyear(file)  # Call the function extractyear() defined in the environment with input
                                            # the full pathname stored in variable file and examined in the current
                                            # iteration

            # 9. Rearrange the order of columns
            # df.columns.tolist() = ['Year', 'Rank', 'Baby_1', 'Baby_2']

            #10. Store the dataframe to a dictionary as the value which key is the name of the file
            pattern = re.compile(r'.*(baby\d\d\d\d).*')
            filename = re.search(pattern, file).group(1)
            dicnames[filename] = df

    # 11. Combine the dataframes stored in the dictionary dicname to an aggregate dataframe dfnames
        for key, value in dicnames.iteritems():
             dfnames = pd.concat[dfnames, value] 

    # 12. Store the dfnames and dicname in a list called result.  Return result.
        result = [dfnames, dicnames]
        return result

Когда я запускаю функцию с заданным путем (путь к моему каталогу, в котором я сохранил файлы html), я получаю следующее сообщение об ошибке:

result = babynames(path)

Вне:

---------------------------------------------------------------------------


file:  C:/Users/ALEX/MyFiles/JUPYTER NOTEBOOKS/google-python-exercises/babynames/baby1990.html
cells[0]:  cells[0] : NaN
cells[1]:  cells[1] : NaN
cells[2]:  cells[2] : NaN
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-72-5c9ebdc4dcdb> in <module>()
----> 1 result = babynames(path)

<ipython-input-71-a0263a6790da> in babynames(path)
     54 
     55                 # 7. Append the lists to the empty dataframe df
---> 56                 df["Rank"] = Rank
     57                 df["Baby_1"] = Baby_1
     58                 df["Baby_2"] = Baby_2

C:\users\alex\Anaconda2\lib\site-packages\pandas\core\frame.pyc in __setitem__(self, key, value)
   2355         else:
   2356             # set column
-> 2357             self._set_item(key, value)
   2358 
   2359     def _setitem_slice(self, key, value):

C:\users\alex\Anaconda2\lib\site-packages\pandas\core\frame.pyc in _set_item(self, key, value)
   2421 
   2422         self._ensure_valid_index(value)
-> 2423         value = self._sanitize_column(key, value)
   2424         NDFrame._set_item(self, key, value)
   2425 

C:\users\alex\Anaconda2\lib\site-packages\pandas\core\frame.pyc in _sanitize_column(self, key, value)
   2576 
   2577             # turn me into an ndarray
-> 2578             value = _sanitize_index(value, self.index, copy=False)
   2579             if not isinstance(value, (np.ndarray, Index)):
   2580                 if isinstance(value, list) and len(value) > 0:

C:\users\alex\Anaconda2\lib\site-packages\pandas\core\series.pyc in _sanitize_index(data, index, copy)
   2768 
   2769     if len(data) != len(index):
-> 2770         raise ValueError('Length of values does not match length of ' 'index')
   2771 
   2772     if isinstance(data, PeriodIndex):

ValueError: Length of values does not match length of index

Ячейки[0], ячейки1 и ячейки[2] должны иметь значения.

Как я уже упоминал, есть еще одна предшествующая таблица, идентифицированная следующим html-кодом:

<table width="100%" border="0" cellspacing="0" cellpadding="4">

Я запускал версию функции, в которой я не указывал таблицу — я не заметил, что в html-файле было две таблицы. В той версии я не получил этот тип ошибки. У меня были сообщения об ошибках для строки 6 о том, что идентификация операторов try была неправильной, чего я не понимаю, и сообщение об ошибке для строки 9, где я пытаюсь переупорядочить столбцы фрейма данных, которые я также не мог понять.

Ваш совет будет оценен.


person im7    schedule 11.01.2017    source источник
comment
попробуй right_table.find_all("tr"). Замените findAll() на find_all()   -  person Mohammad Yusuf    schedule 11.01.2017


Ответы (1)


right_table — это экземпляр ResultSet (по сути, список экземпляров Tag, представляющих элементы), у него нет методов findAll() или find_all().

Вместо этого вы либо перебираете элементы в right_table, если у вас их несколько:

right_table = soup.find_all("table", summary_ = "formatting")

for table in right_table:
    for row in table.findAll("tr"):
        # ...

Или используйте find(), если он один:

right_table = soup.find("table", summary_ = "formatting")

Или используйте один селектор CSS:

for row in soup.select("table[summary=formatting] tr"):
    # ...
person alecxe    schedule 11.01.2017