Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse <img> tags for crawling, if the option ParseImageTags is set. #70

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ type Options struct {

// Extender is the implementation of hooks to use by the crawler.
Extender Extender

// ParseImageTags specifies whether to parse <img> tags and include in the crawling
ParseImageTags bool
}

// NewOptions creates a new set of Options with default values
Expand All @@ -96,5 +99,6 @@ func NewOptions(ext Extender) *Options {
DefaultNormalizationFlags,
LogError,
ext,
false,
}
}
11 changes: 11 additions & 0 deletions worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ func handleBaseTag(root *url.URL, baseHref string, aHref string) string {
var (
aHrefMatcher = cascadia.MustCompile("a[href]")
baseHrefMatcher = cascadia.MustCompile("base[href]")
imgSrcMatcher = cascadia.MustCompile("img[src]")
)

// Scrape the document's content to gather all links
Expand All @@ -397,6 +398,16 @@ func (w *worker) processLinks(doc *goquery.Document) (result []*url.URL) {
}
return val
})
if w.opts.ParseImageTags {
imgURLs := doc.FindMatcher(imgSrcMatcher).Map(func(_ int, s *goquery.Selection) string {
val, _ := s.Attr("src")
if baseURL != "" {
val = handleBaseTag(doc.Url, baseURL, val)
}
return val
})
urls = append(urls, imgURLs...)
}
for _, s := range urls {
// If href starts with "#", then it points to this same exact URL, ignore (will fail to parse anyway)
if len(s) > 0 && !strings.HasPrefix(s, "#") {
Expand Down