anaskhan96 / soup

Web Scraper in Go, similar to BeautifulSoup
MIT License
2.18k stars 168 forks source link

how can I get the nested element? #18

Closed sbb520 closed 6 years ago

sbb520 commented 6 years ago

I want to get a element that contain other element. Such like this: html:

<div id="view">
hello
<p>hello</p>
</div>

go:

doc := soup.HTMLParse(html)
text := doc.Find("div", "id", "view").Text()
fmt.Println(text)

In this sample, it just output "hello". I want it to output "hello\<p>hello\</p>". How can I do that? Thanks for having a look.

anaskhan96 commented 6 years ago

This is because Text() returns the TextNode inside div, which consists only of the data "hello". The rest is a type of an ElementNode (p) which has its own TextNode inside with the data "hello". You'll need to extract the ElementNode first and then call Text() on it.

ebraheemijaz commented 6 years ago

is there any way to get nested text, just like in beautiful soap??