golang 网络爬虫爬取阿里云的镜像中心获取所有镜像信息
下面是一个简单的示例代码,使用 golang 编写了一个网络爬虫,可以爬取阿里云的镜像中心,并获取所有镜像信息。
package main
import (
"fmt"
"io/ioutil"
"net/http"
"regexp"
)
func main() {
url := "https://developer.aliyun.com/mirror/"
resp, err := http.Get(url)
if err != nil {
fmt.Println("Error fetching page:", err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading page:", err)
return
}
// 使用正则表达式匹配镜像信息
re := regexp.MustCompile(`(?s)<div class="mirror-item">.*?<div class="mirror-item-detail">.*?<h4>(.*?)</h4>.*?<p>(.*?)</p>.*?</div>.*?</div>`)
matches := re.FindAllStringSubmatch(string(body), -1)
// 输出所有镜像信息
for _, match := range matches {
fmt.Println("镜像名称:", match[1])
fmt.Println("镜像地址:", match[2])
fmt.Println()
}
}
运行以上代码,即可获取到阿里云镜像中心的所有镜像信息
原文地址: https://www.cveoy.top/t/topic/fhXb 著作权归作者所有。请勿转载和采集!