如何在Go语言中获取属性href的值

huangapple go评论90阅读模式
英文:

how to get value of attribute href value in Go language

问题

我想从HTML内容中解析锚链接。
/* 我的HTML内容示例

<a class="productnamecolor colors_productname" href="http://www.cccxcxc.com/Nautical-Bubble-Romper-p/s15brpnt03.htm">*/
      <a class="productnamecolor colors_productname" href="http://www.dewewe.com/Nautical-Bubble-Romper-p/erewrwer.htm">
    <a class="productnamecolor colors_productname" href="http://www.sdsddsd.com/Nautical-Bubble-Romper-p/dsadadasd.htm"> 

*/
锚点有href属性,我想获取href的值。但是这给了我一个错误..

错误:在单值上下文中的多值s.Attr()

package main
    
    import (
      "fmt"
      "log"
    
      "github.com/PuerkitoBio/goquery"
    )
    
    func ExampleScrape() {
      doc, err := goquery.NewDocument("http://www.myurl.com/category-s/1828.htm") 
      if err != nil {
        log.Fatal(err)
      }
    
    /* **打开http后我的示例HTML** <a class="productnamecolor colors_productname" href="http://www.cccxcxc.com/Nautical-Bubble-Romper-p/s15brpnt03.htm">*/
      <a class="productnamecolor colors_productname" href="http://www.dewewe.com/Nautical-Bubble-Romper-p/erewrwer.htm">
    <a class="productnamecolor colors_productname" href="http://www.sdsddsd.com/Nautical-Bubble-Romper-p/dsadadasd.htm"> ***/
    
    doc.Find("table.v65-productDisplay a.productnamecolor").Each(func(i int, s *goquery.Selection) {
        band := s.Attr("href") // 在这里我想获取属性"href"的值,但这里不起作用。
        fmt.Printf(band)
      })
    }
    
    func main() {
      ExampleScrape()
    }
英文:

I want to parse anchor link from the html content.
/* My HTML Content Sample

<a class="productnamecolor colors_productname" href="http://www.cccxcxc.com/Nautical-Bubble-Romper-p/s15brpnt03.htm">*/
      <a class="productnamecolor colors_productname" href="http://www.dewewe.com/Nautical-Bubble-Romper-p/erewrwer.htm">
    <a class="productnamecolor colors_productname" href="http://www.sdsddsd.com/Nautical-Bubble-Romper-p/dsadadasd.htm"> 

*/
The Anchor have href and i want to get the value of Href . But this is giving me error..

Error: multiple-value s.Attr() in single-value context

package main
    
    import (
      "fmt"
      "log"
    
      "github.com/PuerkitoBio/goquery"
    )
    
    func ExampleScrape() {
      doc, err := goquery.NewDocument("http://www.myurl.com/category-s/1828.htm") 
      if err != nil {
        log.Fatal(err)
      }
    
    /* **my sample html after http open** <a class="productnamecolor colors_productname" href="http://www.cccxcxc.com/Nautical-Bubble-Romper-p/s15brpnt03.htm">*/
      <a class="productnamecolor colors_productname" href="http://www.dewewe.com/Nautical-Bubble-Romper-p/erewrwer.htm">
    <a class="productnamecolor colors_productname" href="http://www.sdsddsd.com/Nautical-Bubble-Romper-p/dsadadasd.htm"> ***/
    
    doc.Find("table.v65-productDisplay a.productnamecolor").Each(func(i int, s *goquery.Selection) {
        band := s.Attr("href") // here i want to get attribute " href " value. this is not working here.
        fmt.Printf(band)
      })
    }
    
    func main() {
      ExampleScrape()
    }

答案1

得分: 10

Selection.Attr 返回两个值:属性值和一个布尔值,指示属性是否存在(如果为假,则属性值为空)。

Go 不喜欢忽略多个返回值,所以你需要将代码更改为以下形式:

doc.Find("table.v65-productDisplay a.productnamecolor").Each(func(i int, s *goquery.Selection) {
    band, ok := s.Attr("href")
    if ok {
        fmt.Printf(band)
    }
})
英文:

Selection.Attr returns two values: the attribute value, and a boolean stating whether the attribute existed or not (the attribute value will be the empty if this is false).

Go doesn't like it when you ignore multiple return values, so you'll have to change your code to the following:

doc.Find("table.v65-productDisplay a.productnamecolor").Each(func(i int, s *goquery.Selection) {
    band, ok := s.Attr("href")
    if ok {
        fmt.Printf(band)
    }
})

答案2

得分: 3

你也可以使用golang.org/pkg/net/html包。

package main

import (
	"fmt"
	"log"
	"strings"

	"golang.org/x/net/html"
)

func main() {
	s := `<a class="productnamecolor colors_productname" href="http://www.cccxcxc.com/Nautical-Bubble-Romper-p/s15brpnt03.htm">
      <a class="productnamecolor colors_productname" href="http://www.dewewe.com/Nautical-Bubble-Romper-p/erewrwer.htm">
    <a class="productnamecolor colors_productname" href="http://www.sdsddsd.com/Nautical-Bubble-Romper-p/dsadadasd.htm">`
	doc, err := html.Parse(strings.NewReader(s))
	if err != nil {
		log.Fatal(err)
	}
	var f func(*html.Node)
	f = func(n *html.Node) {
		if n.Type == html.ElementNode && n.Data == "a" {
			for _, a := range n.Attr {
				if a.Key == "href" {
					fmt.Println(a.Val)
					break
				}
			}
		}
		for c := n.FirstChild; c != nil; c = c.NextSibling {
			f(c)
		}
	}
	f(doc)
}
// 输出结果
// http://www.cccxcxc.com/Nautical-Bubble-Romper-p/s15brpnt03.htm
// http://www.dewewe.com/Nautical-Bubble-Romper-p/erewrwer.htm
// http://www.sdsddsd.com/Nautical-Bubble-Romper-p/dsadadasd.htm

希望对某人有所帮助。

英文:

You can also use the golang.org/pkg/net/html package.

package main

import (
	&quot;fmt&quot;
	&quot;log&quot;
	&quot;strings&quot;

	&quot;golang.org/x/net/html&quot;
)

func main() {
	s := `&lt;a class=&quot;productnamecolor colors_productname&quot; href=&quot;http://www.cccxcxc.com/Nautical-Bubble-Romper-p/s15brpnt03.htm&quot;&gt;*/
      &lt;a class=&quot;productnamecolor colors_productname&quot; href=&quot;http://www.dewewe.com/Nautical-Bubble-Romper-p/erewrwer.htm&quot;&gt;
    &lt;a class=&quot;productnamecolor colors_productname&quot; href=&quot;http://www.sdsddsd.com/Nautical-Bubble-Romper-p/dsadadasd.htm&quot;&gt;`
	doc, err := html.Parse(strings.NewReader(s))
	if err != nil {
		log.Fatal(err)
	}
	var f func(*html.Node)
	f = func(n *html.Node) {
		if n.Type == html.ElementNode &amp;&amp; n.Data == &quot;a&quot; {
			for _, a := range n.Attr {
				if a.Key == &quot;href&quot; {
					fmt.Println(a.Val)
					break
				}
			}
		}
		for c := n.FirstChild; c != nil; c = c.NextSibling {
			f(c)
		}
	}
	f(doc)
}
// outputs
// http://www.cccxcxc.com/Nautical-Bubble-Romper-p/s15brpnt03.htm
// http://www.dewewe.com/Nautical-Bubble-Romper-p/erewrwer.htm
// http://www.sdsddsd.com/Nautical-Bubble-Romper-p/dsadadasd.htm

Hope this helps someone.

huangapple
  • 本文由 发表于 2015年8月24日 05:02:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/32171498.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定