[Question] Read value from html input tag

blov · · 670 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<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(&#34;http://someurl.com&#34;) </code></pre> <p>I&#39;m after:</p> <pre><code>&lt;input id=&#34;foo&#34; value=&#34;bar&#34; /&gt; </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>&lt;input id=&#34;foo&#34; value=&#34;bar&#34; name=&#34;name&#34; /&gt; </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(&#34;name&#34;) </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&#39;s an example form, which has a value that I want:</p> <p>```</p> <pre><code>&lt;form method=&#34;post&#34; target=&#34;_self&#34;&gt; &lt;input name=&#34;authenticity_token&#34; type=&#34;hidden&#34; value=&#34;1LsJBwjnPEBNnJyq5tWjUtNRBscHijWMkFe74YjlgKw=&#34; /&gt; &lt;p&gt; </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 &amp;&amp; n.Data == &#34;meta&#34; { for _, a := range n.Attr { if a.Key == &#34;name&#34; &amp;&amp; a.Val == &#34;csrf-token&#34; { 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

670 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传