diff --git a/version/collector.go b/version/collector.go index 8a7cc78..8daed5d 100644 --- a/version/collector.go +++ b/version/collector.go @@ -3,6 +3,7 @@ package version import ( "fmt" "net/http" + stdurl "net/url" "strings" "github.com/PuerkitoBio/goquery" @@ -38,14 +39,21 @@ func (e *URLUnreachableError) Error() string { // Collector go版本信息采集器 type Collector struct { - url string - doc *goquery.Document + url string + pURL *stdurl.URL + doc *goquery.Document } // NewCollector 返回采集器实例 func NewCollector(url string) (*Collector, error) { + pURL, err := stdurl.Parse(url) + if err != nil { + return nil, err + } + c := Collector{ - url: url, + url: url, + pURL: pURL, } if err := c.loadDocument(); err != nil { return nil, err @@ -71,9 +79,13 @@ func (c *Collector) findPackages(table *goquery.Selection) (pkgs []*Package) { table.Find("tr").Not(".first").Each(func(j int, tr *goquery.Selection) { td := tr.Find("td") + href := td.Eq(0).Find("a").AttrOr("href", "") + if strings.HasPrefix(href, "/") { // relative paths + href = fmt.Sprintf("%s://%s%s", c.pURL.Scheme, c.pURL.Host, href) + } pkgs = append(pkgs, &Package{ FileName: td.Eq(0).Find("a").Text(), - URL: td.Eq(0).Find("a").AttrOr("href", ""), + URL: href, Kind: td.Eq(1).Text(), OS: td.Eq(2).Text(), Arch: td.Eq(3).Text(),