python 可以用html和css写界面_如何在python的HTML页面中剥离整个HTML,CSS和JS代码或标签...

Say I am a very complex HTML page consisting usual HTML tags, CSS & JS in the middle. We might see all worst cases.

All I want is strip all the above tags/ code and return "text".

In simple terms:

This might contain JS, CSS etc. etc..

I am trying to use BeautifulSoup but its not removing JS from the code.. Now ,I am thinking to use Regex.. but not sure how to do

edit1

Here is my try on a simple bootstrap html page...

from bs4 import BeautifulSoup as bs

import requests

bs( requests.get(MY-URL).text ).get_text()

$ return text

html

Home

Le styles

body {

padding-top: 10%;

padding-left: 30%;

}

HTML5 shim, for IE6-8 support of HTML5 elements

[if lt IE 9]>

Home | Under Construction

Sample Page 1

The app

might

face some ........

Firefox

. Ple..

/container

var _gaq = _gaq || [];

_gaq.push(['_trackPageview']);

(function() {

var ga = do...............

})();

解决方案

Django using this function to strip tags from text:

def strip_tags(value):

"""Returns the given HTML with all tags stripped."""

return re.sub(r']*?>', '', force_unicode(value))

(You won't need the force_unicode part)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值