提交 0650e245 编写于 作者: V voidint

Fixbug: 因golang官网网页HTML元素变动,导致无法正常下载安装包。#33

上级 bbeedd24
...@@ -3,6 +3,7 @@ package version ...@@ -3,6 +3,7 @@ package version
import ( import (
"fmt" "fmt"
"net/http" "net/http"
stdurl "net/url"
"strings" "strings"
"github.com/PuerkitoBio/goquery" "github.com/PuerkitoBio/goquery"
...@@ -38,14 +39,21 @@ func (e *URLUnreachableError) Error() string { ...@@ -38,14 +39,21 @@ func (e *URLUnreachableError) Error() string {
// Collector go版本信息采集器 // Collector go版本信息采集器
type Collector struct { type Collector struct {
url string url string
doc *goquery.Document pURL *stdurl.URL
doc *goquery.Document
} }
// NewCollector 返回采集器实例 // NewCollector 返回采集器实例
func NewCollector(url string) (*Collector, error) { func NewCollector(url string) (*Collector, error) {
pURL, err := stdurl.Parse(url)
if err != nil {
return nil, err
}
c := Collector{ c := Collector{
url: url, url: url,
pURL: pURL,
} }
if err := c.loadDocument(); err != nil { if err := c.loadDocument(); err != nil {
return nil, err return nil, err
...@@ -71,9 +79,13 @@ func (c *Collector) findPackages(table *goquery.Selection) (pkgs []*Package) { ...@@ -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) { table.Find("tr").Not(".first").Each(func(j int, tr *goquery.Selection) {
td := tr.Find("td") 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{ pkgs = append(pkgs, &Package{
FileName: td.Eq(0).Find("a").Text(), FileName: td.Eq(0).Find("a").Text(),
URL: td.Eq(0).Find("a").AttrOr("href", ""), URL: href,
Kind: td.Eq(1).Text(), Kind: td.Eq(1).Text(),
OS: td.Eq(2).Text(), OS: td.Eq(2).Text(),
Arch: td.Eq(3).Text(), Arch: td.Eq(3).Text(),
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册