本文整理汇总了Golang中github.com/PuerkitoBio/goquery.Selection.Attr方法的典型用法代码示例。如果您正苦于以下问题:Golang Selection.Attr方法的具体用法?Golang Selection.Attr怎么用?Golang Selection.Attr使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/PuerkitoBio/goquery.Selection
的用法示例。
在下文中一共展示了Selection.Attr方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: score
func score(tag *goquery.Selection) int {
src, _ := tag.Attr("src")
if src == "" {
src, _ = tag.Attr("data-src")
}
if src == "" {
src, _ = tag.Attr("data-lazy-src")
}
if src == "" {
return -1
}
tagScore := 0
for rule, score := range rules {
if rule.MatchString(src) {
tagScore += score
}
}
alt, exists := tag.Attr("alt")
if exists {
if strings.Contains(alt, "thumbnail") {
tagScore--
}
}
id, exists := tag.Attr("id")
if exists {
if id == "fbPhotoImage" {
tagScore++
}
}
return tagScore
}
示例2: guessSourceURL
func guessSourceURL(s *goquery.Selection, link *url.URL) string {
possibleSrcAttr := []string{"src",
"data-src",
"srcset",
"data-full-size",
"data-original",
"href", // bbc
"pagespeed_lazy_src",
}
var possibleSrcURLs []string
for _, attr := range possibleSrcAttr {
link, exist := s.Attr(attr)
if exist {
Debug("possible image src url under: %v", attr)
possibleSrcURLs = append(possibleSrcURLs, link)
}
}
if len(possibleSrcURLs) == 0 {
return ""
}
validSrcURLs := removeInvalidURLs(possibleSrcURLs, link)
if len(validSrcURLs) == 0 {
return ""
}
indexLongestElement, _ := longestElement(validSrcURLs)
return validSrcURLs[indexLongestElement]
}
示例3: attrOrDefault
// attributeOrDefault reads an attribute and returns it or the default value when it's empty.
func (bow *Browser) attrOrDefault(name, def string, sel *goquery.Selection) string {
a, ok := sel.Attr(name)
if ok {
return a
}
return def
}
示例4: processLink
// Add street to result and find street information (to WikipediaMoscow.result)
func (parser *WikipediaMoscow) processLink(_ int, s *goquery.Selection, done chan<- *StreetInfo) {
name := strings.TrimSpace(s.Text())
if len(name) == 0 {
done <- parser.getEmptyInfo("")
return
}
href, exists := s.Attr("href")
if !exists {
done <- parser.getEmptyInfo("")
return
}
var (
class string
info *StreetInfo
)
class, exists = s.Attr("class")
if exists && class == "new" {
info = parser.getEmptyInfo(name)
} else if resp, err := http.Get(parser.baseURL + href); err != nil {
info = parser.getEmptyInfo(name)
} else {
streetparser := NewWikipediaStreetParser()
info, err = streetparser.ParseStreetInfo(name, resp.Body)
if err != nil {
info = parser.getEmptyInfo(name)
}
}
done <- info
}
示例5: name
func (this *parser) name(selector string, selection *goquery.Selection) string {
value, exists := selection.Attr(selector)
if exists {
return value
}
return ""
}
示例6: toPage
// toPage is a helper function that accepts an anchor
// tag referencing a markdown file, parsing the markdown
// file and returning a page to be included in our docs.
func toPage(site *Site, el *goquery.Selection) (*Page, error) {
// follow the link to see if this is a page
// that should be added to our documentation.
href, ok := el.Attr("href")
if !ok || href == "#" {
return nil, nil
}
// read the markdown file, convert to html and
// read into a dom element.
doc, err := toDocument(filepath.Join(site.base, href))
if err != nil {
return nil, err
}
// convert the extension from markdown to
// html, in preparation for type conversion.
href = strings.Replace(href, ".md", ".html", -1)
el.SetAttr("href", href)
page := &Page{}
page.Href = href
page.html, err = doc.Html()
return page, err
}
示例7: getSrc
func (ve *VideoExtractor) getSrc(node *goquery.Selection) string {
value, exists := node.Attr("src")
if exists {
return value
}
return ""
}
示例8: convertTagToJqueryFormat
func convertTagToJqueryFormat(tag string, s *goquery.Selection) string {
tagitself := tag
pos := strings.Index(tag, " ")
if pos > -1 {
tagitself = tag[0:pos]
} else {
return tag
}
class, found := s.Attr("class")
if found && class != "" {
pos := strings.Index(class, " ")
// leave only a first class from a list
if pos > -1 {
class = class[0:pos]
}
tagitself = tagitself + "." + class
}
return tagitself
}
示例9: classWeight
func (d *Document) classWeight(s *goquery.Selection) int {
weight := 0
if !d.WeightClasses {
return weight
}
class, _ := s.Attr("class")
id, _ := s.Attr("id")
if class != "" {
if negativeRegexp.MatchString(class) {
weight -= 25
}
if positiveRegexp.MatchString(class) {
weight += 25
}
}
if id != "" {
if negativeRegexp.MatchString(id) {
weight -= 25
}
if positiveRegexp.MatchString(id) {
weight += 25
}
}
return weight
}
示例10: Parse
// Parse from div.tweet
func (tweet *Tweet) Parse(s *goquery.Selection) (err error) {
success := false
attrs := []string{
"data-item-id",
"data-screen-name",
"data-name",
}
data := map[string]string{}
for _, attr := range attrs {
var value string
if value, success = s.Attr(attr); !success {
tweet.Success = 0
err = fmt.Errorf("not having %s attribute", attr)
return
}
data[attr] = value
}
tweet.ItemID = data["data-item-id"]
tweet.ScreenName = data["data-screen-name"]
tweet.Name = data["data-name"]
tweet.Success = 1
// if could get the above attribues, allow the following values to be blank.
tweet.Time, _ = s.Find("._timestamp").Attr("data-time")
tweet.Text = s.Find(".tweet-text").Text()
return
}
示例11: ScrapeNotes
func ScrapeNotes(s *goquery.Selection) []string {
notes := []string{}
s.Find("abbr").Not("abbr:first-of-type").Not("abbr.c").Each(func(i int, s *goquery.Selection) {
note, _ := s.Attr("title")
notes = append(notes, note)
})
return notes
}
示例12: ScrapeOrigins
func ScrapeOrigins(s *goquery.Selection) []string {
origins := []string{}
s.Find("abbr.c").Each(func(i int, s *goquery.Selection) {
origin, _ := s.Attr("title")
origins = append(origins, origin)
})
return origins
}
示例13: ScrapeNotes
func ScrapeNotes(s *goquery.Selection) []string {
notes := []string{}
s.Clone().Find("span[title]").First().Remove().End().End().Find("span.d i span.d[title]").Remove().End().Find("span.d[title]").Each(func(i int, s *goquery.Selection) {
note, _ := s.Attr("title")
notes = append(notes, note)
})
return notes
}
示例14: attrToUrl
func attrToUrl(s *goquery.Selection, attr string) (*url.URL, error) {
link, exists := s.Attr(attr)
if exists {
return url.Parse(link)
}
return nil, errors.New("Attr " + attr + " not found")
}
示例15: getHeight
func (ve *VideoExtractor) getHeight(node *goquery.Selection) int {
value, exists := node.Attr("height")
if exists {
nvalue, _ := strconv.Atoi(value)
return nvalue
}
return 0
}