To scrape product details from JD Central Thailand, you’ll need to target the HTML elements containing product names, prices, and availability. JD Central’s product listings typically contain structured data inside specific class attributes. Scrapy makes it easy to extract this data using CSS selectors. Handling pagination correctly is essential to scrape products across multiple pages, which can be done using Scrapy’s response.follow() method to navigate through links.
import scrapy
class JDSpider(scrapy.Spider):
name = 'jd_spider'
start_urls = ['https://www.jd.co.th/th/search?query=laptop']
def parse(self, response):
for product in response.xpath('//div[@class="product-item"]'):
title = product.xpath('.//div[@class="product-name"]/text()').get()
price = product.xpath('.//span[@class="product-price"]/text()').get()
availability = product.xpath('.//span[@class="product-availability"]/text()').get()
yield {
'title': title.strip(),
'price': price.strip(),
'availability': availability.strip(),
}
next_page = response.xpath('//a[@class="next"]/@href').get()
if next_page:
yield response.follow(next_page, self.parse)