func (s Scraper) SearchParts(searchTerm string, region string) ([]SearchPart, error) {
	if region != "" {
		region += "."
	}

	fullURL := "https://" + region + "pcpartpicker.com/search?q=" + url.QueryEscape(searchTerm)

	if !MatchPCPPURL(fullURL) {
		return nil, errors.New("invalid region")
	}

	searchResults := []SearchPart{}

	var reqURL string

	s.Collector.OnHTML(".pageTitle", func(h *colly.HTMLElement) {
		reqURL = h.Request.URL.String()
	})

	s.Collector.OnHTML(".search-results__pageContent .block", func(el *colly.HTMLElement) {
		el.ForEach(".list-unstyled li", func(i int, searchResult *colly.HTMLElement) {
			vendorURL := linkURL("https://", el.Request.URL.Host, searchResult.ChildAttr(".search_results--price a", "href"))
			stringPrice := searchResult.ChildText(".search_results--price a")

			price, curr, _ := StringPriceToFloat(stringPrice)

			vendorName := ""

			if stringPrice != "" {
				vendorName = ExtractVendorName(vendorURL)
			}

			partVendor := Vendor{
				URL:  vendorURL,
				Name: vendorName,
				Price: Price{
					Total:       price,
					TotalString: stringPrice,
					Currency:    curr,
				},
				InStock: len(stringPrice) > 0,
			}

			searchResults = append(searchResults, SearchPart{
				Name:   searchResult.ChildText(".search_results--link a"),
				Image:  linkURL("https:", searchResult.ChildAttr(".search_results--img a img", "src")),
				URL:    linkURL("https://", el.Request.URL.Host, searchResult.ChildAttr(".search_results--link a", "href")),
				Vendor: partVendor,
			})
		})
	})

	err := s.Collector.Visit(fullURL)
	s.Collector.Wait()

	if err != nil {
		return nil, err
	}

	if MatchProductURL(reqURL) {
		return nil, &RedirectError{
			URL: reqURL,
		}
	}

	return searchResults, nil
}
// Searches for parts using PCPartPicker's search function.
Searches for parts using PCPartPicker's search function.
// Searches for parts using PCPartPicker ' s search function .
func (s Scraper) SearchParts(searchTerm string, region string) ([]SearchPart, error) {
	if region != "" {
		region += "."
	}
	fullURL := "https://" + region + "pcpartpicker.com/search?q=" + url.QueryEscape(searchTerm)
	if !MatchPCPPURL(fullURL) {
		return nil, errors.New("invalid region")
	}
	searchResults := []SearchPart{}
	var reqURL string
	s.Collector.OnHTML(".pageTitle", func(h *colly.HTMLElement) {
		reqURL = h.Request.URL.String()
	})
	s.Collector.OnHTML(".search-results__pageContent .block", func(el *colly.HTMLElement) {
		el.ForEach(".list-unstyled li", func(i int, searchResult *colly.HTMLElement) {
			vendorURL := linkURL("https://", el.Request.URL.Host, searchResult.ChildAttr(".search_results--price a", "href"))
			stringPrice := searchResult.ChildText(".search_results--price a")
			price, curr, _ := StringPriceToFloat(stringPrice)
			vendorName := ""
			if stringPrice != "" {
				vendorName = ExtractVendorName(vendorURL)
			}
			partVendor := Vendor{
				URL:  vendorURL,
				Name: vendorName,
				Price: Price{
					Total:       price,
					TotalString: stringPrice,
					Currency:    curr,
				},
				InStock: len(stringPrice) > 0,
			}
			searchResults = append(searchResults, SearchPart{
				Name:   searchResult.ChildText(".search_results--link a"),
				Image:  linkURL("https:", searchResult.ChildAttr(".search_results--img a img", "src")),
				URL:    linkURL("https://", el.Request.URL.Host, searchResult.ChildAttr(".search_results--link a", "href")),
				Vendor: partVendor,
			})
		})
	})
	err := s.Collector.Visit(fullURL)
	s.Collector.Wait()
	if err != nil {
		return nil, err
	}
	if MatchProductURL(reqURL) {
		return nil, &RedirectError{
			URL: reqURL,
		}
	}
	return searchResults, nil
}
func ( s Scraper ) SearchParts ( searchTerm string , region string ) ( [ ] SearchPart , error ) { if region != " " { region += " " } fullURL := " " + region + " " + url . QueryEscape ( searchTerm ) if ! MatchPCPPURL ( fullURL ) { return nil , errors . New ( " " ) } searchResults := [ ] SearchPart { } var reqURL string s . Collector . OnHTML ( " " , func ( h * colly . HTMLElement ) { reqURL = h . Request . URL . String ( ) } ) s . Collector . OnHTML ( " " , func ( el * colly . HTMLElement ) { el . ForEach ( " " , func ( i int , searchResult * colly . HTMLElement ) { vendorURL := linkURL ( " " , el . Request . URL . Host , searchResult . ChildAttr ( " " , " " ) ) stringPrice := searchResult . ChildText ( " " ) price , curr , _ := StringPriceToFloat ( stringPrice ) vendorName := " " if stringPrice != " " { vendorName = ExtractVendorName ( vendorURL ) } partVendor := Vendor { URL : vendorURL , Name : vendorName , Price : Price { Total : price , TotalString : stringPrice , Currency : curr , } , InStock : len ( stringPrice ) > 0 , } searchResults = append ( searchResults , SearchPart { Name : searchResult . ChildText ( " " ) , Image : linkURL ( " " , searchResult . ChildAttr ( " " , " " ) ) , URL : linkURL ( " " , el . Request . URL . Host , searchResult . ChildAttr ( " " , " " ) ) , Vendor : partVendor , } ) } ) } ) err := s . Collector . Visit ( fullURL ) s . Collector . Wait ( ) if err != nil { return nil , err } if MatchProductURL ( reqURL ) { return nil , & RedirectError { URL : reqURL , } } return searchResults , nil }
Searches for parts using PCPartPicker's search function.
Searches for parts using PCPartPicker ' s search function .