ResultSet object has no attribute find_all

2024/9/29 23:34:56

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'})
Answer

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 resultsprice = result.find('div', attrs={'class':'price'}).text[13:-11]records.append((name, price,))
https://en.xdnf.cn/q/71152.html

Related Q&A

How can I set path to load data from CSV file into PostgreSQL database in Docker container?

I would like to load data from CSV file into PostgreSQL database in Docker. I run:docker exec -ti my project_db_1 psql -U postgresThen I select my database:\c myDatabaseNow I try to load data from myfi…

Why is this simple Spark program not utlizing multiple cores?

So, Im running this simple program on a 16 core multicore system. I run it by issuing the following.spark-submit --master local[*] pi.pyAnd the code of that program is the following. #"""…

How to get python dictionaries into a pandas time series dataframe where key is date object

I have a python dictionaries where the key is a dateobject and the value is the timeseires.timeseries = {datetime.datetime(2013, 3, 17, 18, 19): {t2: 400, t1: 1000},datetime.datetime(2013, 3, 17, 18, 2…

Changing the color of an image based on RGB value

Situation:You have an image with 1 main color and you need to convert it to another based on a given rgb value.Problem:There are a number of different, but similar shades of that color that also need …

Python NumPy - FFT and Inverse FFT?

Ive been working with FFT, and Im currently trying to get a sound waveform from a file with FFT, (modify it eventually), but then output that modified waveform back to a file. Ive gotten the FFT of the…

Tools to help developers reading class hierarchy faster

I mostly spend time on Python/Django and Objective-C/CocoaTouch and js/jQuery in the course of my daily work.My editor of choice is vim for Python/Django and js/jQuery and xcode for Objective-C/CocoaTo…

Python Last Iteration in For Loop [duplicate]

This question already has answers here:What is the pythonic way to detect the last element in a for loop?(34 answers)How do I read and write CSV files?(7 answers)Closed 9 months ago.Is there any simp…

Django 1.7 multisite User model

I want to serve a Django application that serves multiple web sites by single database but different user sets. Think like a blog application, it will be used by several domains with different themes, …

Does for key in dict in python always iterate in a fixed order?

Does the python codefor key in dict:..., where dict is a dict data type, always iterate in a fixed order with regrard to key? For example, suppose dict={"aaa":1,"bbb",2}, will the …

Kinesis Firehose lambda transformation

I have the following lambda function as part of Kinesis firehose record transformation which transforms msgpack record from the kinesis input stream to json.Lambda Runtime: python 3.6from __future__ im…