When scraping Lazada Thailand, make sure you’re handling the request headers properly. The site may block requests that don’t appear to come from an actual browser, so it’s essential to mimic a real browser using headers. In addition, the structure of the HTML might change across different product categories, so using flexible selectors is a good approach. Always keep an eye on the terms of service of any site you scrape and ensure you’re in compliance.
import requests
from bs4 import BeautifulSoup
url = 'https://www.lazada.co.th/catalog/?q=shoes'
headers = {'User-Agent': 'Mozilla/5.0'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
# Parse and print product details
products = soup.find_all('div', {'class': 'c1ZEkM'})
for product in products:
title = product.find('div', {'class': 'c16H9d'}).text.strip()
price = product.find('span', {'class': 'c13VH6'}).text.strip()
print(f'Title: {title}, Price: {price}')