亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Go 訪問 DOM 并獲取數據

使用 Go 訪問 DOM 并獲取數據

Go
慕仙森 2023-01-03 17:00:08
我想從 URL 訪問 HTML 文檔標簽,例如,我有以下網頁:https://example.com/h1我想要標簽“Example Domain”中的內部內容:<h1>Example Domain</h1><p>標簽相同:<p> More information...</p>然后使用來自不同標簽的值創建一個結構:type Example struct {   foo string   bar string}Example.foo = *h1 tag content*Example.bar = *p tag content*這可能嗎?
查看完整描述

1 回答

?
HUX布斯

TA貢獻1876條經驗 獲得超6個贊

我個人會為此使用goquery

// Request the HTML page.

res, err := http.Get("https://example.com/")

if err != nil {

    log.Fatal(err)

}

defer res.Body.Close()

if res.StatusCode != 200 {

    log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)

}


// Load the HTML document

doc, err := goquery.NewDocumentFromReader(res.Body)

if err != nil {

    log.Fatal(err)

}


h1 := doc.Find("h1").First().Text()

p := doc.Find("p").First().Text()


type Example struct {

    foo string

    bar string

}


e := Example{ foo: h1, bar: p }


查看完整回答
反對 回復 2023-01-03
  • 1 回答
  • 0 關注
  • 110 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號