Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ResultSet object has no attribute 'find_all'

i always met one problem, when I scraping one web page.

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

anyone can tell me how to solve this? my code as below:

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 = results.find('div', attrs={'class':'name'}).text 
    price = results.find('div', attrs={'class':'price'}).text[13:-11]
    records.append((name, price,))

I want to ask a close question.If I want to scrap multiple pages.the pattern like below,I use the code as below,but still scrap the first page only Can you solve this issue.

import requests  
for i in range(100):   
    url = "https://www.example.com/a/a_{}.format(i)"
    r = requests.get(url)
from bs4 import BeautifulSoup  
soup = BeautifulSoup(r.text, 'html.parser')  
results = soup.find_all('div', attrs={'class':'product-item item-template-0 alternative'})
like image 920
Yan Zhang Avatar asked Feb 27 '18 06:02

Yan Zhang


People also ask

What is a ResultSet in Python?

ResultSet: The actual data asked for in the query when using a fetch method such as . fetchall() on a ResultProxy.

How do you convert a tag to a string in Python?

To convert a Tag object to a string in Beautiful Soup, simply use str(Tag) .


1 Answers

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,))
like image 142
whackamadoodle3000 Avatar answered Nov 03 '22 18:11

whackamadoodle3000