Skip to content

Commit

Permalink
Fixbug: 因golang官网网页HTML元素变动,导致无法正常下载安装包。#33
Browse files Browse the repository at this point in the history
  • Loading branch information
voidint committed Jul 3, 2020
1 parent bbeedd2 commit 0650e24
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions version/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package version
import (
"fmt"
"net/http"
stdurl "net/url"
"strings"

"github.com/PuerkitoBio/goquery"
Expand Down Expand Up @@ -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
Expand All @@ -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(),
Expand Down

0 comments on commit 0650e24

Please sign in to comment.