GoLang – GoQuery HTML插入失败

huangapple go评论79阅读模式
英文:

GoLang - GoQuery HTML Insertion Fails

问题

我希望提取elementB,然后在elementCelementD之前停止提取,也就是说,不提取elementCelementD.text内容。然而,我只知道如何提取整个div的文本,使用Contents().Not来忽略elementC,但是elementD仍然被提取了。

这是我目前正在使用的代码:

GoLang:

capturedText := s.Find("div").Contents().Not(".label").Text()

这个代码忽略了elementC,但是没有忽略没有外部标签的elementD

HTML:

<li><span><h2>elementA</h2></span><div>elementB<br><span class="label">elementC</span>elementD</div></li>

我该如何只提取<div>中的elementB,而不包括elementCelementD

编辑:

我尝试了关闭div标签,像这样:

s.Find(".label").BeforeHtml(`</div>`)

还尝试了:

s.Find(".label").BeforeHtml(`</div><div>`)

并且尝试访问第一个div,忽略第二个div,第二个div现在应该有elementD

jp, _ := s.Find("div").First().Html()

然而,这并没有起作用。似乎</div>不能是一个开放标签 - 它需要是<div>...</div>才能正确插入。但这不是我需要的,我只需要</div></div><div>来正确关闭第一个div。

请问应该如何修复这个问题?

英文:

I wish to extract elementB, and then stop before element C and D - i.e., do not extract the .text of content elementC and elementD. However, I only know how to extract the entire div text, using Contents().Not to ignore elementC, but elementD is still captured.

Here is the code I am currently using:

GoLang:

capturedText := s.Find(&quot;div&quot;).Contents().Not(&quot;.label&quot;).Text()

Which ignores elementC, but not elementD, which has no outer tags.

HTML:

&lt;li&gt;&lt;span&gt;&lt;h2&gt;elementA&lt;/h2&gt;&lt;/span&gt;&lt;div&gt;elementB&lt;br&gt;&lt;span class=&quot;label&quot;&gt;elementC&lt;/span&gt;elementD&lt;/div&gt;&lt;/li&gt;

How do I capture only elementB of &lt;div&gt;, and not elementC and elementD?

Edit:

I have tried closing the div tag like so:

s.Find(&quot;.label&quot;).BeforeHtml(`&lt;/div&gt;`)

and also tried:

s.Find(&quot;.label&quot;).BeforeHtml(`&lt;/div&gt;&lt;div&gt;`)

and accessing the first div, disregarding the second div which should now have elementD with:

jp, _ := s.Find(&quot;div&quot;).First().Html()

However, this is not working. It seems that &lt;/div&gt; must not be an open tag - it needs to be &lt;div&gt;...&lt;/div&gt; to insert correctly. But this is NOT what I need, I require ONLY &lt;/div&gt; or &lt;/div&gt;&lt;div&gt; to close the first div correctly.

What is the appropriate way to fix this?

答案1

得分: 0

由于我无法编辑带有“broken”节点的HTML,所以我选择了以下方法:

s.Find(".label").BeforeHtml(|_SEPARATOR_|) // 在Html中插入文本分隔符
preCleanNode := s.Find("div").Contents().Not(".label").Text() // 将Html转换为文本
cleanNode := strings.Split(preCleanNode, |_SEPARATOR_|) // 根据文本分隔符拆分文本
outputString := cleanNode[0] // 输出我们想要的文本

英文:

Since I can't edit the HTML with a 'broken' node, I have opted for this:

	s.Find(&quot;.label&quot;).BeforeHtml(`|_SEPARATOR_|`) // Insert text separator into Html
	preCleanNode := s.Find(&quot;div&quot;).Contents().Not(&quot;.label&quot;).Text() //Get Html as Text
	cleanNode := strings.Split(preCleanNode, `|_SEPARATOR_|`) // Split text based on Text Separator
	outputString := cleanNode[0] // Output our wanted text

huangapple
  • 本文由 发表于 2017年2月10日 00:47:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/42142333.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定