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

Is there a way to get the index of the current a[href] element? #211

Closed
hsluoyz opened this issue Aug 20, 2018 · 4 comments
Closed

Is there a way to get the index of the current a[href] element? #211

hsluoyz opened this issue Aug 20, 2018 · 4 comments

Comments

@hsluoyz
Copy link

hsluoyz commented Aug 20, 2018

I want to know the index of the current a[href] element for the page, as follows.

c.OnHTML("a[href]", func(e *colly.HTMLElement) {
	index := ...
	fmt.Println("This is %d-th hyperlink for this page", index)
})

Is there a way to do it? Thanks.

@asciimoo
Copy link
Member

You can get it by calling e.DOM.index()

@hsluoyz
Copy link
Author

hsluoyz commented Aug 22, 2018

@asciimoo , but I printed it out, it's not correct. See the following output. The indentation shows the depth. The meaning of [x-y-z] is:

  • x: request ID
  • y: the actual index of <a href=“xx”></a> I manually recorded
  • z: e.DOM.index()

Most values of e.DOM.index()are 0, sometimes 1. This is not correct.

[0-0-0] /
  [1-0-0] /blog/
    [2-0-0] /feed.html/
      [3-7-0] /contact.html/
    [2-1-0] /blog/*/*/*.html/
      [5-1-0] /blog/*/
        [6-3-0] /blog/*/*/
      [5-14-0] /road/
        [8-7-0] /road/*/*.html/
          [9-1-0] /road/*/
      [5-15-1] /survivor/
        [11-7-0] /survivor/*/*.html/
          [12-1-0] /survivor/*/
      [5-24-0] /support.html/
    [2-26-0] /about.html/
      [15-1-0] /photo.html/
      [15-3-0] /docs/pg/
      [15-4-0] /docs/mjos/

Process finished with exit code 0

@asciimoo asciimoo reopened this Aug 22, 2018
@asciimoo
Copy link
Member

I've added HTMLElement.Index, please verify that it fixes the issue.

@hsluoyz
Copy link
Author

hsluoyz commented Aug 22, 2018

It works. Thanks for the quick fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants