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,


<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) {
  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

Hope this helps.

Speak Your Mind