Help? Error with goquery

xuanbao · · 598 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hello! </p> <p>I&#39;m parsing different rss feeds and I want to get the images out of the html. Here is the feed that&#39;s causing the problem:</p> <p><a href="http://feeds.feedburner.com/ColourloversCoup0Palettes/New?format=xml" rel="nofollow">http://feeds.feedburner.com/ColourloversCoup0Palettes/New?format=xml</a></p> <p>I&#39;m taking the description field and extracting the img tag. Here is the code:</p> <pre><code>doc, err := goquery.NewDocument(item.Description) if err != nil { fmt.Println(err) } doc.Find(&#34;img&#34;).Each(func(i int, s *goquery.Selection) { str, exists := s.Attr(&#34;src&#34;) if exists { u, err := url.Parse(str) checkError(err) m, _ := url.ParseQuery(u.RawQuery) fmt.Println(m) } else { fmt.Println(s.Text()) } }) </code></pre> <p>It gives me this error: </p> <p>Time To Think</p> <p>Get %3Cimg%20src=%22<a href="http://www.colourlovers.com/paletteRSS/CF6EF5/1FF29C/88E1D6/F5779B/B4159D/Time_To_Think.png%22%20style=%22width:%20168px;%20height:%2067px;%20border:%201px%20solid%20#d0d0d0;%20padding:%202px;%22%20alt=%22CF6EF5/1FF29C/88E1D6/F5779B/B4159D%22%20/%3E:" rel="nofollow">http://www.colourlovers.com/paletteRSS/CF6EF5/1FF29C/88E1D6/F5779B/B4159D/Time_To_Think.png%22%20style=%22width:%20168px;%20height:%2067px;%20border:%201px%20solid%20#d0d0d0;%20padding:%202px;%22%20alt=%22CF6EF5/1FF29C/88E1D6/F5779B/B4159D%22%20/%3E:</a> unsupported protocol scheme &#34;&#34; panic: runtime error: invalid memory address or nil pointer dereference [signal 0xb code=0x1 addr=0x0 pc=0x31e2]</p> <p>goroutine 1 [running]: panic(0x3e8940, 0xc82000a0d0) /usr/local/go/src/runtime/panic.go:481 +0x3e6 main.ranged(0x0, 0x0, 0xc8201c4169, 0x3, 0xc8201c41a8, 0x3, 0xc8201f0600, 0xa, 0x10) /Users/Kaly/code/src/github.com/misamisa23/decode/decode.go:138 +0x452 main.main() /Users/Kaly/code/src/github.com/misamisa23/decode/decode.go:186 +0x208</p> <p>Any ideas?</p> <hr/>**评论:**<br/><br/>beeker1121: <pre><p>The error &#39;invalid memory address or nil pointer dereference&#39; refers to you trying to dereference a pointer that is nil.</p> <p>For instance, say you issued an http.Get response.</p> <pre><code>resp, err := http.Get(&#34;http://example.com&#34;) if err != nil { fmt.Println(err) } </code></pre> <p>Now, if there was an error and you tried to dereference and use a variable in resp, you would receive this error as resp is a nil pointer (http.Get returns nil for *http.Response, and an error) and as such has no members or variables (trying to read resp.StatusCode for instance will produce this error).</p> <p>What it could be is you&#39;re trying to use the <code>doc</code> variable after checking for a possible error. If there was an error creating a NewDocument with goquery, <code>doc</code> will be nil and calling <code>doc.Find()</code> or anything else on <code>doc</code> will result in the error.</p> <p>Check the lines the output shows too:</p> <p>/Users/Kaly/code/src/github.com/misamisa23/decode/decode.go - line 138</p> <p>/Users/Kaly/code/src/github.com/misamisa23/decode/decode.go - line 186</p></pre>beeker1121: <pre><p>Also, looks like the URL you are trying to issue a GET request on is incorrect - it&#39;s trying to GET on the entire &lt;img&gt; element instead of just the URL.</p></pre>mc_hammerd: <pre><p>just guessing but that url may have an error theres a space (or <code>+</code>) <code>%20</code> and then <code>style= &#34;&#34;</code> so maybe the url is not 100%; maybe its doing &#34;GET /&lt;img src=foo.png style=blah&#34; (invalid http)</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

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