Dharmesh Rupani Dharmesh Rupani - 4 years ago 89
Ruby Question

How to remove selective <script> tag among multiple <script> tags?

I have multiple

<script>
tags on a webpage. I want to remove a few
<script>
tags from among them. How can I do using Nokogiri?

For example, I have five
<script>
tags:

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


I want to remove only
"script file 2"
and
"script file B"
.

Answer Source

You could find the corresponding nodes with xpath and remove them :

doc.xpath("//script[@src='script file 2' or @src='script file B']").remove
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download