-
Use Go to scrape product prices from PChome Taiwan
How would you scrape product prices from PChome, one of Taiwan’s leading e-commerce websites? Are the prices located in a specific HTML element that is consistent across all products? Or does the page structure vary depending on the product category? What about dynamically loaded content—are the prices rendered directly in the HTML or fetched via an API after the page loads?
Would Go, along with the Colly library, be suitable for this task? Colly is lightweight and efficient for scraping static content, but does it handle dynamic content well? If the prices are loaded via JavaScript, should the scraper fetch and parse the API response instead of relying on HTML elements? Here’s a potential script that might work—does it address these challenges?
Does this script properly identify and handle the price elements? If the prices are loaded dynamically, would adding API scraping or a headless browser improve accuracy? How should edge cases, like missing prices or formatting variations, be handled? These are all considerations for building a robust scraper for PChome.package main import ( "fmt" "log" "github.com/gocolly/colly" ) func main() { // Create a new Colly collector c := colly.NewCollector() // Scrape product prices c.OnHTML(".price-section", func(e *colly.HTMLElement) { price := e.Text fmt.Println("Product Price:", price) }) // Handle errors c.OnError(func(_ *colly.Response, err error) { log.Println("Error occurred:", err) }) // Visit the PChome product page err := c.Visit("https://24h.pchome.com.tw/product-page") if err != nil { log.Fatalf("Failed to visit website: %v", err) } }
Log in to reply.