0
votes

I'm trying to scrape product's of online store by BeautifulSoup but i have a problem, There is only one product i can scrape! , but i want to scrape all product's.

import requests
from bs4 import BeautifulSoup
res = requests.get('https://www.digikala.com/search/category-wearable-gadget/')
soup = BeautifulSoup(res.text, 'html.parser')
result = soup.find_all('div', class_='c-product-box__content')
first_watch = result[0]
first_name = first_watch.a.text
first_rate = first_watch.find(class_='c-product-box__rate-comparision--rate-people')
first_price = first_watch.find(class_='c-price__value-wrapper')
first_rate = first_rate.text
first_price = first_price.text

As soon as I use first_watch = result and use find_all showing this error :

Traceback (most recent call last): File "C:/Users/Oogway/PycharmProjects/web_scraping1/web_s.py", line 8, in first_name = first_watch.a.text File "C:\Users\Oogway.virtualenvs\web_scraping1\lib\site-packages\bs4\element.py", line 2160, in__getattr__ raise AttributeError( AttributeError: ResultSet object has no attribute 'a'. 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
res = requests.get('https://www.digikala.com/search/category-wearable-gadget/')
soup = BeautifulSoup(res.text, 'html.parser')
result = soup.find_all('div', class_='c-product-box__content')
first_watch = result
first_name = first_watch.a.text
first_rate = first_watch.find_all(class_='c-product-box__rate-comparision--rate-people')
first_price = first_watch.find_all(class_='c-price__value-wrapper')
first_rate = first_rate.text
first_price = first_price.text
1
FYI It’s scrape not scrap - balmy

1 Answers

0
votes

You need to iterate the result object.

import requests
from bs4 import BeautifulSoup
res = requests.get('https://www.digikala.com/search/category-wearable-gadget/')
soup = BeautifulSoup(res.text, 'html.parser')
result = soup.find_all('div', class_='c-product-box__content')
for itm in result:
    print(itm.a.text)

find_all method will return all the div with class c-product-box__content.