Skip to content
Advertisement

how to scrape all items of a page using a loop

i’m trying to scrape and list 24 items from a certain website. instead, it shows the same item 24 times.

to get the first price i did this:

gamma_html = uClient.read()
uClient.close()
page_soup = soup(gamma_html, "html.parser")
hout = page_soup.findAll ("article",{"class":"mc-product-tile product-online-state-for_sale_not_orderable_in_stock product-state-for_sale"})
print len(hout)
i = 0
contain = hout[i]
brand = contain.div.img["alt"]
price = contain.findAll("div",{"class":"product-price-current"})
price = str(price).split("<div class="product-price-current">")[1]
price = price.split(".</div>")[0]
price_2 = contain.findAll("span",{"class":"product-price-decimal"})
price_2 = str(price_2).split("<span class="product-price-decimal">")[1]
price_2 = price_2.split("</span>")[0]
price_3 = (price + "," +  price_2)
product = brand + price_3
print(product)
for i in range(24):
        print(product)

as said, it does print 24 lines. but instead of listing all the different ones, it just lists the first result (probably because the i = 0). i’m very bad at coding so I don’t know what to change to get the wanted result. any thoughts?

Advertisement

Answer

Try the below code.

from bs4 import BeautifulSoup
import requests
url = "https://www.gamma.nl/assortiment/l/hout/bouwhout/hardhout"
html = requests.get(url)
soup = BeautifulSoup(html.text, 'html.parser')

for item in soup.select("article.mc-product-tile"):
    print("title : " + item.select_one("div.product-name a").text)
    print("Price : " + item.select_one("div.wishlist-icon")['data-productprice'].strip())

Output:

title : Lat hardhout geschaafd wit gegrond 9x116 mm 210 cm
Price : 16.99
title : Lat geschaafd hardhout 12x55 mm 210 cm
Price : 8.49
title : Lat hardhout geschaafd 20x28 mm 210 cm
Price : 9.59
title : Plank eiken 19x195 mm 250 cm
Price : 29.49
title : Lat hardhout geschaafd 28x44 mm 210 cm
Price : 14.99
title : Lat hardhout geschaafd 9x116 mm 210 cm
Price : 14.99
title : Lat hardhout geschaafd wit gegrond 12x68 mm 210 cm
Price : 14.99
title : Lat hardhout geschaafd 20x44 mm 210 cm
Price : 12.49
title : Glaslat hardhout wit gegrond 17x28 mm 270 cm
Price : 10.99
title : Lat hardhout geschaafd wit gegrond 12x44 mm 210 cm
Price : 9.99
title : Lat hardhout geschaafd 12x68 mm 210 cm
Price : 12.99
title : Lat hardhout geschaafd 20x68 mm 210 cm
Price : 15.99
title : Lat hardhout geschaafd 9x35 mm 210 cm
Price : 6.79
title : Lat geschaafd hardhout 12x12 mm 270 cm
Price : 3.29
title : Lat hardhout geschaafd 12x35 mm 210 cm
Price : 8.29
title : Lat geschaafd hardhout 9x44 mm 270 cm
Price : 7.49
title : Lat geschaafd hardhout 12x27 mm 270 cm
Price : 6.99
title : Lat hardhout geschaafd 12x116 mm 210 cm
Price : 19.49
title : Lat hardhout geschaafd 9x28 mm 210 cm
Price : 4.49
title : Lat hardhout geschaafd 44x44 mm 210 cm
Price : 24.99
title : Lat geschaafd hardhout 12x44 mm 270 cm
Price : 8.99
title : Lat geschaafd hardhout 9x27 mm 270 cm
Price : 5.19
title : Lat hardhout geschaafd 44 x 68 mm 210 cm
Price : 43.98
title : Lat hardhout geschaafd 20x116 mm 210 cm
Price : 34.95
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement