-
Compare Ruby and Go to scrape shipping details from Yahoo! Taiwan
How does scraping shipping details from Yahoo! Taiwan differ when using Ruby versus Go? Is Ruby’s Nokogiri gem easier to implement for parsing HTML, or does Go’s Colly library provide better performance for large-scale scraping? How do both languages handle dynamically loaded content, such as shipping costs or estimated delivery times that might depend on user interactions?
Below are two potential implementations—one in Ruby and one in Go—to scrape shipping details from Yahoo! Taiwan. Which approach is better suited for the task at hand, and which would you choose for scalability and ease of maintenance?Ruby Implementation:require 'nokogiri' require 'open-uri' # URL of the Yahoo! Taiwan product page url = 'https://tw.buy.yahoo.com/product-page' # Fetch the page content doc = Nokogiri::HTML(URI.open(url)) # Scrape shipping details shipping_section = doc.at_css('.shipping-info') if shipping_section shipping_cost = shipping_section.at_css('.cost')&.text&.strip || 'No shipping cost available' delivery_time = shipping_section.at_css('.time')&.text&.strip || 'No delivery time specified' puts "Shipping Cost: #{shipping_cost}" puts "Delivery Time: #{delivery_time}" else puts "Shipping details not found." end
Go Implementation:
package main import ( "fmt" "log" "github.com/gocolly/colly" ) func main() { // Create a new Colly collector c := colly.NewCollector() // Scrape shipping details c.OnHTML(".shipping-info", func(e *colly.HTMLElement) { cost := e.ChildText(".cost") time := e.ChildText(".time") if cost == "" { cost = "No shipping cost available" } if time == "" { time = "No delivery time specified" } fmt.Printf("Shipping Cost: %s\nDelivery Time: %s\n", cost, time) }) // Handle errors c.OnError(func(_ *colly.Response, err error) { log.Println("Error occurred:", err) }) // Visit the Yahoo! Taiwan product page err := c.Visit("https://tw.buy.yahoo.com/product-page") if err != nil { log.Fatalf("Failed to visit website: %v", err) } }
Log in to reply.