<p>I need to read the value from an html input tag and have googled myself blue in the face but found nothing to complete this trivial task.</p>
<p>My code so far:</p>
<pre><code>response, _ := http.Get("http://someurl.com")
</code></pre>
<p>I'm after:</p>
<pre><code><input id="foo" value="bar" />
</code></pre>
<hr/>**评论:**<br/><br/>fr4nk3n: <pre><p><a href="https://github.com/PuerkitoBio/goquery" rel="nofollow">Goquery</a> works pretty good. </p></pre>M_3bdelRahman: <pre><p>add name attribute inside input tag</p>
<pre><code><input id="foo" value="bar" name="name" />
</code></pre>
<p>and use <a href="http://golang.org/pkg/net/http/#Request.FormValue" rel="nofollow">FormValue</a> function to get value </p>
<pre><code>name := r.FormValue("name")
</code></pre></pre>gogroob: <pre><p>r.FormValue should work for most forms, however if things get more tricky, use <code>x/net/html</code>
<a href="http://godoc.org/golang.org/x/net/html" rel="nofollow">http://godoc.org/golang.org/x/net/html</a></p>
<p>Here's an example form, which has a value that I want:</p>
<p>```</p>
<pre><code><form method="post" target="_self">
<input name="authenticity_token" type="hidden" value="1LsJBwjnPEBNnJyq5tWjUtNRBscHijWMkFe74YjlgKw=" />
<p>
</code></pre>
<p>```</p>
<p>I used net/html here:</p>
<p>```</p>
<pre><code>// setToken parses an http.Response for x-csrf-token
func (c *Client) setToken(resp *http.Response) error {
// parse html for x-csrf-token
var f func(*html.Node)
doc, err := html.Parse(resp.Body)
if err != nil {
return err
}
f = func(n *html.Node) {
if n.Type == html.ElementNode && n.Data == "meta" {
for _, a := range n.Attr {
if a.Key == "name" && a.Val == "csrf-token" {
token := n.Attr[0].Val
// set Token
c.Token = token
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
f(c)
}
}
f(doc)
return nil
}
</code></pre>
<p>```</p></pre>albatr0s: <pre><p>You may use this:</p>
<p><a href="https://github.com/yhat/scrape" rel="nofollow">https://github.com/yhat/scrape</a></p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传