我在网页上有多个< script>
标记。我想从中删除一些< script>
标记。我如何使用Nokogiri?
例如,我有五个< script>
标签:
&lt; script type =“text / javascript”src =“script file 1”&gt;&lt; / script&gt;&#xA;&lt; script type =“text / javascript”src =“脚本文件2“&gt;&lt; / script&gt;&#xA;&lt; script type =”text / javascript“src =”脚本文件A“&gt;&lt; / script&gt;&#xA;&lt; script type =”text / javascript“src =”脚本文件B“&gt;&lt; / script&gt;&#xA;&lt; script type =”text / javascript“src =”脚本文件C“&gt;&lt; / script&gt;&#xA; < / code>
&#xA;&#xA; 我只想删除“脚本文件2”
和“脚本文件B”
。< / p>&#XA;
答案 0 :(得分:0)
答案 1 :(得分:0)
使用CSS我可能会使用类似的东西:
require 'nokogiri'
doc = Nokogiri::HTML::DocumentFragment.parse(<<EOT)
<script type="text/javascript" src="script file 1"></script>
<script type="text/javascript" src="script file 2"></script>
<script type="text/javascript" src="script file A"></script>
<script type="text/javascript" src="script file B"></script>
<script type="text/javascript" src="script file C"></script>
EOT
doc.search('script').select { |script| script['src'][/file [2B]$/] }.map(&:remove)
puts doc.to_html
# >> <script type="text/javascript" src="script%20file%201"></script>
# >>
# >> <script type="text/javascript" src="script%20file%20A"></script>
# >>
# >> <script type="text/javascript" src="script%20file%20C"></script>