Issue
[python] Iteration for products not working! just doing this for a single page... URL in link (Lazada Website given)
its working for a single item but not in a loop!
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup
url = 'https://www.lazada.com.ph/tag/tv/?q=tv&_keyori=ss&from=input&spm=a2o4l.home.search.go.7564ca18N2HNBJ&catalog_redirect_tag=true'
driver = webdriver.Chrome()
driver.get(url)
driver.implicitly_wait(3)
soup = BeautifulSoup(driver.page_source,"html.parser")
for item in soup.findAll('div',class_='Bm3ON'):
title = soup.find('div',class_='RfADt').text
price = soup.find('div',class_='aBrP0').text
sold = soup.find('div',class_='_6uN7R').text
print(title,price,sold)
#for i in product:
driver.close()
Solution
You want to find the title, price and sold on the item
that soup.findAll()
gave you. Right now with soup.find()
you're searching for elements in the entire page, not just in the current product, so you will always get the information of the first product of the page. Also you should print on each iteration, not after the loop, otherwise you will only print the information for the last item:
for item in soup.findAll('div', class_='Bm3ON'):
title = item.find('div', class_='RfADt').text
price = item.find('div', class_='aBrP0').text
sold = item.find('div', class_='_6uN7R').text
print(title, price, sold)
Answered By - Ada
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.