How to replace an HTML element with another using goquery in Golang?

How do I replace <span class="e">Heading</span> with the <pre> tags in Golang. That is,

Input:

<span class="e"> Heading </span>

Desired output:

<pre> Heading <pre>

For context, I’m crawling a page using colly. I want to convert the custom <span> classes to <pre> so it can be rendered properly. I tried ReplaceWith("pre") and it doesn’t work and removes the span without replacing it with pre.

c.OnHTML("span.e", func (e *colly.HTMLElement) {
		e.DOM.ReplaceWith("<pre>")
})
  1. Accepted Answer

Accepted Answer

Use the ReplaceWithHtml function of goquery

To do this in goquery, you can use the ReplaceWithHtml function. It replaces each element in the set of matched elements with the parsed HTML. It returns the removed elements.

func (s *Selection) ReplaceWithHtml(htmlStr string) *Selection

In your case,

c.OnHTML("span.e", func (e *colly.HTMLElement) {
		text:= e.DOM.Text() // extract the text between <span>s
		e.DOM.ReplaceWithHtml("<pre>"+text+"</pre>")
})

Hope this helps.

Read free Software Algorithms and Computer Science Books by CodeAhoy Learn.

Introduction to Recursion and Backtracking

Sorting Algorithms

Introduction to C Programming Language

And More...

Speak Your Mind