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

Get text after an html node (outside of an html element) #287

Closed
wangsongyan opened this issue Mar 8, 2019 · 2 comments
Closed

Get text after an html node (outside of an html element) #287

wangsongyan opened this issue Mar 8, 2019 · 2 comments

Comments

@wangsongyan
Copy link

is there a way to get text after html node?such as 'hangzhou',' join Jul 31, 2017'

<div class="description">
	<i class="octicon octicon-location"></i> hangzhou
	<i class="octicon octicon-mail"></i>
	<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>
	<i class="octicon octicon-clock"></i> join Jul 31, 2017
</div>
@mna
Copy link
Member

mna commented Mar 8, 2019

Hello,

Since those are text nodes, not elements (or "tags"), you can't get to them with a selector. What you can do is something like that:

        doc.Find(".description").Contents().Each(func(i int, s *goquery.Selection) {
		if goquery.NodeName(s) == "#text" {
			fmt.Printf(">>> (%d) >>> %s\n", i, s.Text())
		}
	})

I.e. Contents gets all nodes inside the description div (including text nodes), and NodeName gets either the "tag" for element nodes, or the special "#text" string for text nodes, so you can get to them that way.

Hope this helps!
Martin

@wangsongyan
Copy link
Author

Thanks it helped!

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