[英]How to get the div tag that is loaded after sometime using Nokogiri
[英]How to add text after div using Nokogiri
我正在嘗試在h3
標簽之后添加一行文本。
我的起始HTML是這樣的:
<h3>hi</h3>
沒有其他標簽或文檔類型。 我希望結尾的HTML看起來像這樣:
<h3>hi</h3>
{% include 'tester-credit-button' %}
我嘗試了以下方法:
page = Nokogiri::HTML::DocumentFragment.parse("my_page.html")
title = page.at_css("h3")
#tried this first
title.children.after("\n{% include 'tester-credit-button' %}")
#then this
title << "\n{% include 'tester-credit-button' %}"
#then this
title.after("\n{% include 'tester-credit-button' %}")
#then this
text_node = Nokogiri::XML::Text.new("\n{% include 'tester-credit-button' %}"
, page)
title.add_child(text_node)
您將其變得太難了:
require 'nokogiri'
doc = Nokogiri::HTML::DocumentFragment.parse('<h3>hi</h3>')
doc.at('h3').next = "\n{% include 'tester-credit-button' %}"
puts doc.to_html
# >> <h3>hi</h3>
# >> {% include 'tester-credit-button' %}
返回您的代碼...
page = Nokogiri::HTML::DocumentFragment.parse("my_page.html")
是錯的。 您可以使用以下方法輕松進行測試:
page.to_html # => "my_page.html"
在文檔中 ,如果顯示“ tags
”,則意味着您必須傳遞一些HTML或XML:
page = Nokogiri::HTML::DocumentFragment.parse("<p><span>foo</span></p>")
page.to_html # => "<p><span>foo</span></p>"
我建議研究HTML和XML的結構,因為您的實驗表明您不了解已解析文檔的節點,其可能的類型以及其子級和同級的概念。 Nokogiri的教程將有所幫助,在SO上搜索標簽並閱讀各種問題和答案也將有所幫助。 一旦掌握了Nokogiri的功能,它便功能強大且易於使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.