Funny Boss
Funny Boss

Reputation: 338

Getting error AttributeError: ResultSet object has no attribute 'find_all'

Hi I am trying to find out all the links under pagination thing and the pagination part code already extracted. but when i was trying to capture all the list items I am getting the following error:

AttributeError: ResultSet object has no attribute 'find_all'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?

import requests
from bs4 import BeautifulSoup

url = "https://scrapingclub.com/exercise/list_basic/"

response = requests.get(url)
soup = BeautifulSoup(response.text, 'lxml')

pages = soup.find_all('ul', class_='pagination')
links = pages.find_all('a', class_='page-link')
print(links)

enter image description here

I did not understand by the term AttributeError: ResultSet object has no attribute 'find_all'. can anybody check this what I am missing.

Upvotes: 0

Views: 617

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195448

The problem is you cannot call .find_all() or .find() on ResultSet returned by first .find_all() call.

This example will print all links from pagination:

import requests
from bs4 import BeautifulSoup

url = "https://scrapingclub.com/exercise/list_basic/"

response = requests.get(url)
soup = BeautifulSoup(response.text, 'lxml')

pages = soup.find('ul', class_='pagination')          # <-- .find() to return only one element

for link in pages.find_all('a', class_='page-link'):  # <-- find_all() to return list of elements
    print(link)

Prints:

<a class="page-link" href="?page=2">2</a>
<a class="page-link" href="?page=3">3</a>
<a class="page-link" href="?page=4">4</a>
<a class="page-link" href="?page=5">5</a>
<a class="page-link" href="?page=6">6</a>
<a class="page-link" href="?page=7">7</a>
<a class="page-link" href="?page=2">Next</a>

Upvotes: 1

Related Questions