我想從 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 }
- 1 回答
- 0 關注
- 110 瀏覽
添加回答
舉報
0/150
提交
取消