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:
JavaScript
x
20
20
1
gamma_html = uClient.read()
2
uClient.close()
3
page_soup = soup(gamma_html, "html.parser")
4
hout = page_soup.findAll ("article",{"class":"mc-product-tile product-online-state-for_sale_not_orderable_in_stock product-state-for_sale"})
5
print len(hout)
6
i = 0
7
contain = hout[i]
8
brand = contain.div.img["alt"]
9
price = contain.findAll("div",{"class":"product-price-current"})
10
price = str(price).split("<div class="product-price-current">")[1]
11
price = price.split(".</div>")[0]
12
price_2 = contain.findAll("span",{"class":"product-price-decimal"})
13
price_2 = str(price_2).split("<span class="product-price-decimal">")[1]
14
price_2 = price_2.split("</span>")[0]
15
price_3 = (price + "," + price_2)
16
product = brand + price_3
17
print(product)
18
for i in range(24):
19
print(product)
20
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.
JavaScript
1
10
10
1
from bs4 import BeautifulSoup
2
import requests
3
url = "https://www.gamma.nl/assortiment/l/hout/bouwhout/hardhout"
4
html = requests.get(url)
5
soup = BeautifulSoup(html.text, 'html.parser')
6
7
for item in soup.select("article.mc-product-tile"):
8
print("title : " + item.select_one("div.product-name a").text)
9
print("Price : " + item.select_one("div.wishlist-icon")['data-productprice'].strip())
10
Output:
JavaScript
1
49
49
1
title : Lat hardhout geschaafd wit gegrond 9x116 mm 210 cm
2
Price : 16.99
3
title : Lat geschaafd hardhout 12x55 mm 210 cm
4
Price : 8.49
5
title : Lat hardhout geschaafd 20x28 mm 210 cm
6
Price : 9.59
7
title : Plank eiken 19x195 mm 250 cm
8
Price : 29.49
9
title : Lat hardhout geschaafd 28x44 mm 210 cm
10
Price : 14.99
11
title : Lat hardhout geschaafd 9x116 mm 210 cm
12
Price : 14.99
13
title : Lat hardhout geschaafd wit gegrond 12x68 mm 210 cm
14
Price : 14.99
15
title : Lat hardhout geschaafd 20x44 mm 210 cm
16
Price : 12.49
17
title : Glaslat hardhout wit gegrond 17x28 mm 270 cm
18
Price : 10.99
19
title : Lat hardhout geschaafd wit gegrond 12x44 mm 210 cm
20
Price : 9.99
21
title : Lat hardhout geschaafd 12x68 mm 210 cm
22
Price : 12.99
23
title : Lat hardhout geschaafd 20x68 mm 210 cm
24
Price : 15.99
25
title : Lat hardhout geschaafd 9x35 mm 210 cm
26
Price : 6.79
27
title : Lat geschaafd hardhout 12x12 mm 270 cm
28
Price : 3.29
29
title : Lat hardhout geschaafd 12x35 mm 210 cm
30
Price : 8.29
31
title : Lat geschaafd hardhout 9x44 mm 270 cm
32
Price : 7.49
33
title : Lat geschaafd hardhout 12x27 mm 270 cm
34
Price : 6.99
35
title : Lat hardhout geschaafd 12x116 mm 210 cm
36
Price : 19.49
37
title : Lat hardhout geschaafd 9x28 mm 210 cm
38
Price : 4.49
39
title : Lat hardhout geschaafd 44x44 mm 210 cm
40
Price : 24.99
41
title : Lat geschaafd hardhout 12x44 mm 270 cm
42
Price : 8.99
43
title : Lat geschaafd hardhout 9x27 mm 270 cm
44
Price : 5.19
45
title : Lat hardhout geschaafd 44 x 68 mm 210 cm
46
Price : 43.98
47
title : Lat hardhout geschaafd 20x116 mm 210 cm
48
Price : 34.95
49