Golang“登錄網站并下載xls文件”?
告訴我如何使用 Golang 登錄站點。下載xls文件是得到了,但是為了在Excel表格中有數據,需要登錄網站。該站點位于公司的服務器上。如果你能告訴你怎么做。例如,我用來執(zhí)行此操作的 VBA 代碼。Set oFields = CreateObject("Scripting.Dictionary")With oFields .Add "login", "sdiscor" .Add "password", "sdiscor"End WithFor Each sName In oFields oFields(sName) = sName & "=" & EncodeUriComponent(oFields(sName))NextsPayLoad = Join(oFields.Items(), "&")With CreateObject("MSXML2.XMLHTTP") .Open "POST", "http://effect.gvc.oao.rzd/cgi_bin/effect_access.pl?", False .setRequestHeader "Content-Type", "application/x-www-form-urlencoded" .setRequestHeader "Content-Length", LenB(sPayLoad) .Send (sPayLoad) Do While .readyState <> 4 DoEvents Loop webLink = "http://effect.gvc.oao.rzd/effect/table/***&LOGIN=&PASSWORD=" vLocalFile = ThisWorkbook.Path & "\SIS-Effect.xls" .Open "GET", webLink, True .Send Do While .readyState <> 4 DoEvents Loop oResp = .responseBody vFF = FreeFile If Dir(vLocalFile) <> "" Then Kill vLocalFile Open vLocalFile For Binary As #vFF Put #vFF, , oResp Close #vFFEnd With感謝萊奧拉?。。∽罱K代碼func main() {urlLogin := "http://effect.gvc.oao.rzd/cgi_bin/effect_access.pl?"urlDownload := "http://effect.gvc.oao.rzd/effect/table/OZODO10U.XLS?DAT=2019.03.04&LOGIN=&PASSWORD="cookieJar, _ := cookiejar.New(nil)client := &http.Client{Jar: cookieJar,}_, err := client.PostForm(urlLogin, url.Values{"login": {"sdiscor"}, "password": {"sdiscor"}})if err != nil { fmt.Println("Error while downloading", urlLogin, "-", err) return}fileName := "1.xls"fmt.Println("Downloading", urlDownload, "to", fileName)output, err := os.Create(fileName)if err != nil { fmt.Println("Error while creating", fileName, "-", err) return}defer output.Close()resp, err := client.Get(urlDownload)if err != nil { fmt.Println("Error while downloading", urlDownload, "-", err) return}
查看完整描述