Search code examples
pythonbeautifulsoupfindall

Debugging with find_all()[]


I have to debug code that I haven't written. I think I found the problem in this line, but it is not clear to me what it does. Can anyone explain it to me?

 number_of_items_found = int(soup.find_all('li')[3].text.split(' ')[0])

Solution

  • It seems like you are using BeautifulSoup to scrape an html text. So the soup.find_all('li') part will extract every <li>..</li> tag from your html. Then you access the fourth of those tags (index = 3) and split its text at spaces.

    For example: If the text that the li contains is something like 12 books in your cart then using .split(' ') on it will give you a list: ['12', 'books', 'in', 'your', 'cart'].

    After you access the first element of the list (index = 0) and convert it to an integer. So to continue with the assumed text and list above the outcome will be: int('12') -> 12.

    Hope it makes sense.