Beautiful Soup: 'Resultset' Object Has No Attribute 'Find_All'

Why does ResultSet object has no attribute 'find'?

In your code otherAreasContainer is of type ResultSet, and ResultSet doesn't have .find_all() method.

To select all <li> from under the "Other areas of Wikipedia", you can use CSS selector h2:contains("Other areas of Wikipedia") + div li.

For example:

import requests
from bs4 import BeautifulSoup

url = 'https://en.wikipedia.org/'
soup = BeautifulSoup(requests.get(url).content, 'lxml')

for li in soup.select('h2:contains("Other areas of Wikipedia") + div li'):
print(li.text)

Prints:

Community portal – Bulletin board, projects, resources and activities covering a wide range of Wikipedia areas.
Help desk – Ask questions about using Wikipedia.
Local embassy – For Wikipedia-related communication in languages other than English.
Reference desk – Serving as virtual librarians, Wikipedia volunteers tackle your questions on a wide range of subjects.
Site news – Announcements, updates, articles and press releases on Wikipedia and the Wikimedia Foundation.
Village pump – For discussions about Wikipedia itself, including areas for technical issues and policies.

More about CSS Selectors.

ResultSet object has no attribute 'find_all'

Try this. You mixed up results with result:

import requests  
r = requests.get('https://www.example.com')
from bs4 import BeautifulSoup
soup = BeautifulSoup(r.text, 'html.parser')
results = soup.find_all('div', attrs={'class':'product-item item-template-0 alternative'})
records = []
for result in results:
name = result.find('div', attrs={'class':'name'}).text # result not results
price = result.find('div', attrs={'class':'price'}).text[13:-11]
records.append((name, price,))

AttributeError : ResultSet object has no attribute 'find_all'

p_tags = soup.find_all('p', class_='name') this return a list. List doesn't have a find_all() method. iterate over the items in p_tags to call find_all().

for ptag in p_tags:
a_tags = ptag.find_all('a')
for link in a_tags:
links = link.get('href')
print(links)

Scraping table with BeautifulSoup AttributeError: ResultSet object has no attribute 'find_all'

Each data point under device information also contains a table. So you can use CSS selector to pull the right data.

import requests
from bs4 import BeautifulSoup

url = 'https://www.accessdata.fda.gov/scripts/cdrh/cfdocs/cfpcd/classification.cfm?start_search=1&submission_type_id=&devicename=&productcode=&deviceclass=&thirdparty=&panel=®ulationnumber=&implant_flag=&life_sustain_support_flag=&summary_malfunction_reporting=&sortcolumn=deviceclassdesc&pagenum=10'

r = requests.get(url)

soup = BeautifulSoup(r.text, 'html.parser')

product_tables = soup.select('table tbody tr td a')[11:]
for product_table in product_tables:
print(product_table.get_text(strip=True))

Output:

device, cpr assist
heart valve, more than minimally manipulated allograft
cleanser, root canal
saliva, artificial
locator, root apex
device, electrical dental anesthesia
mouthguard, prescription
cord, retraction
mouthguard, over-the-counter
mouthguard, migraine/tension headache

How to scrape mulitple pages at once, avoiding: 'ResultSet' object has no attribute 'find_all'?

Try to avoid all these lists and loops, it needs only one and will also eliminate the error. Else you have to iterate your ResultSets additionally, but this would not be a good behavior.

for i in range(1,21):
url = f"https://www.autoscout24.de/lst?sort=standard&desc=0&cy=D&atype=C&ustate=N%2CU&powertype=ps&ocs_listing=include&adage=1&page={i}"

page = requests.get(url)
soup = BeautifulSoup(page.content, "html.parser")

for div in soup.find_all("div", class_="VehicleDetailTable_container__mUUbY"):
mileage = div.find_all("span", class_="VehicleDetailTable_item__koEV4")[0].text
year = div.find_all("span", class_="VehicleDetailTable_item__koEV4")[1].text
print(mileage)
print(year)

Be aware Working with this indexes could result in texts that do not match your expectation in case there is no milage or year available. It would be better to scrape the detailpages instead.

Example

from bs4 import BeautifulSoup
import requests

for i in range(1,21):
url = f"https://www.autoscout24.de/lst?sort=standard&desc=0&cy=D&atype=C&ustate=N%2CU&powertype=ps&ocs_listing=include&adage=1&page={i}"

page = requests.get(url)
soup = BeautifulSoup(page.content, "html.parser")

for div in soup.find_all("div", class_="VehicleDetailTable_container__mUUbY"):
mileage = div.find_all("span", class_="VehicleDetailTable_item__koEV4")[0].text
year = div.find_all("span", class_="VehicleDetailTable_item__koEV4")[1].text
print(mileage)
print(year)

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

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>

ResultSet object has no attribute 'find_all'. You're probably treating a list of elements like a single element

Problem is exactly what error message is saying you are treating a list as a single element

import bs4
import lxml
import requests
from bs4 import BeautifulSoup

link=('www.site.com')

def Data():
output = []

#find_all returns a list so use find in a

a=soup.find("div",{'class':'selector'})
b=a.find_all("li")
for element in b:
link = element.attrs.get("data-value", None)
output.append(link)

return output

AttributeError: ResultSet object has no attribute 'find_all'. BeautifulSoup

The error is here: table_rows = tables.find_all('tr'). tables is of type ResultSet, so put it inside a loop. For example:

import requests 
import pandas as pd
from bs4 import BeautifulSoup

URL = "WEBSITE"
r = requests.get(URL)
soup = BeautifulSoup(r.content, 'html5lib')

tables = soup.find_all('table', attrs={'class': 'crDataTable'})

res = []
for table in tables: # <-- add for here
table_rows = table.find_all('tr') # <-- table.
for tr in table_rows:
td = tr.find_all('td')
row = [cell.text.strip() for cell in td if cell.text.strip()]
if row:
res.append(row)

df = pd.DataFrame(res, columns=["","2020","2016","2017","2018","2019",""])
print(df)


Related Topics



Leave a reply



Submit