![](/img/trans.png)
[英]How to read the html tags innerHTML value using css selector or xpath.?
[英]How to reject specify HTML tags by using css or xpath selector
我想通過使用css或xpath選擇器來刪除style
和script
標簽及其內容。
這是一個示例HTML:
<html>
<head>
<title>test</title>
<style>
// style
</style>
<script>
/* some script */
</script>
</head>
<body>
<p>text</p>
<script>
/* some script */
</script>
<div>foo</div>
</body>
</html>
我想要一個這樣的HTML:
<html>
<head>
<title>test</title>
</head>
<body>
<p>text</p>
<div>foo</div>
</body>
</html>
我以為我可以用此代碼獲得不包含<script>
標記的HTML,但是以某種方式該代碼僅復制HTML。
doc = Nokogiri::HTML(open("foo.text"))
doc.css(":not(script)").to_html
如何啟用我想要的行為?
嘗試以下行:
doc.search('.//style').remove
doc.search('.//script').remove
更簡單的是:
doc.search('style,script').remove
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.