当前位置: 首页>>代码示例>>Golang>>正文


Golang Selection.Attr方法代码示例

本文整理汇总了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
}
开发者ID:quipo,项目名称:GoOse,代码行数:33,代码来源:images.go

示例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]
}
开发者ID:frankMilde,项目名称:rol,代码行数:31,代码来源:image.go

示例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
}
开发者ID:haruyama,项目名称:surf,代码行数:8,代码来源:browser.go

示例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
}
开发者ID:Jenyay,项目名称:streetlist,代码行数:34,代码来源:wikipediamoscow.go

示例5: name

func (this *parser) name(selector string, selection *goquery.Selection) string {
	value, exists := selection.Attr(selector)
	if exists {
		return value
	}
	return ""
}
开发者ID:hotei,项目名称:GoOse,代码行数:7,代码来源:parser.go

示例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
}
开发者ID:fclairamb,项目名称:drone,代码行数:29,代码来源:generate-docs.go

示例7: getSrc

func (ve *VideoExtractor) getSrc(node *goquery.Selection) string {
	value, exists := node.Attr("src")
	if exists {
		return value
	}
	return ""
}
开发者ID:hotei,项目名称:GoOse,代码行数:7,代码来源:videos.go

示例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
}
开发者ID:Gelembjuk,项目名称:articletext,代码行数:26,代码来源:selectionpath.go

示例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
}
开发者ID:jpoehls,项目名称:feedmailer,代码行数:31,代码来源:readability.go

示例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
}
开发者ID:sosuke-k,项目名称:twitter-responding-machine,代码行数:30,代码来源:fetch.go

示例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
}
开发者ID:squat,项目名称:drae,代码行数:8,代码来源:scrape.go

示例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
}
开发者ID:squat,项目名称:drae,代码行数:8,代码来源:scrape.go

示例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
}
开发者ID:GrenderG,项目名称:drae,代码行数:8,代码来源:scrape.go

示例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")
}
开发者ID:eriklarko,项目名称:purple-shopper,代码行数:8,代码来源:product-finder.go

示例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
}
开发者ID:hotei,项目名称:GoOse,代码行数:8,代码来源:videos.go


注:本文中的github.com/PuerkitoBio/goquery.Selection.Attr方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。