473,888 Members | 1,499 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stripping scripts from HTML with regular expressions

Hey everyone,

I'm trying to strip all script-blocks from a HTML-file using regex.

I tried the following in Python:

testfile = open('testfile' )
testhtml = testfile.read()
regex = re.compile('<sc ript\b[^>]*>(.*?)</script>', re.DOTALL)
result = regex.sub('', blaat)
print result

This strips far more away then just the script-blocks. Am I missing
something from the regex-implementation from Python or am I doing something
else wrong?

greetz
MFB
Apr 9 '08 #1
3 1604

-----Original Message-----
From: py************* *************** ****@python.org [mailto:python-
li************* ************@py thon.org] On Behalf Of Michel Bouwmans
Sent: Wednesday, April 09, 2008 3:38 PM
To: py*********@pyt hon.org
Subject: Stripping scripts from HTML with regular expressions

Hey everyone,

I'm trying to strip all script-blocks from a HTML-file using regex.

I tried the following in Python:

testfile = open('testfile' )
testhtml = testfile.read()
regex = re.compile('<sc ript\b[^>]*>(.*?)</script>', re.DOTALL)
result = regex.sub('', blaat)
print result

This strips far more away then just the script-blocks. Am I missing
something from the regex-implementation from Python or am I doing
something
else wrong?
[Insert obligatory comment about using a html specific parser
(HTMLParser) instead of regexes.]

Actually your regex didn't appear to strip anything. You probably saw
stuff disappear because blaat != testhtml:
testhtml = testfile.read()
result = regex.sub('', blaat)
Try this:

import re

testfile = open('a.html')
testhtml = testfile.read()
regex = re.compile('<sc ript\s+.*?>(.*? )</script>', re.DOTALL)
result = regex.sub('',te sthtml)

print result


Apr 9 '08 #2
Michel Bouwmans wrote:
I'm trying to strip all script-blocks from a HTML-file using regex.
You might want to take a look at lxml.html instead, which comes with an HTML
cleaner module:

http://codespeak.net/lxml/lxmlhtml.h...eaning-up-html

Stefan
Apr 9 '08 #3
In article <ma************ *************** ***********@pyt hon.org>,
"Reedick, Andrew" <jr****@ATT.COM wrote:
-----Original Message-----
From: py************* *************** ****@python.org [mailto:python-
li************* ************@py thon.org] On Behalf Of Michel Bouwmans
Sent: Wednesday, April 09, 2008 3:38 PM
To: py*********@pyt hon.org
Subject: Stripping scripts from HTML with regular expressions

Hey everyone,

I'm trying to strip all script-blocks from a HTML-file using regex.

[Insert obligatory comment about using a html specific parser
(HTMLParser) instead of regexes.]
Yah, seconded. To the OP - use BeautifulSoup or HtmlData unless you like
to reinvent wheels.

--
Philip
http://NikitaTheSpider.com/
Whole-site HTML validation, link checking and more
Apr 10 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
3453
by: Martin | last post by:
I have a HTML parser that reads product pages from various retailers - and I want to optimize it somewhat: I download all HTML before I start the parsing - and to do that I want to: - Get rid of all HTML parts that I don't need, i.e. <head>, <title>, <javascript> etc. I'm considering using eregi_replace for this. Anyone have an example of how to set this up ? I tried this with no luck:
7
2602
by: YoBro | last post by:
Hi I have used some of this code from the PHP manual, but I am bloody hopeless with regular expressions. Was hoping somebody could offer a hand. The output of this will put the name of a form field beside name. I want to get the following but not sure how to modify the code below. 1. Field Name (to appear beside NAME:) 2. Field Type (to appear beside TYPE:)
11
4907
by: yawnmoth | last post by:
say i have a for loop that would iterate through every character and put a space between every 80th one, in effect forcing word wrap to occur. this can be implemented easily using a regular expression. if i wanted to improve on this, and make it so stuff in url's didn't count towards that 80 character limit, a regular expression would not suffice. however, a simple for loop does. so now i'm currious how to account for html escape...
1
4195
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make regular expressions easier to create and use (and in my experience as a regular expression user, it makes them MUCH easier to create and use.) I'm still working on formal documentation, and in any case, such documentation isn't necessarily the...
4
18597
by: Michael Vilain | last post by:
Originally, I was using $value =~ s/<.*>//g; to strip HTML tags from a variable. It actually stripped everything from the first "<" to the last ">" after the ending tag. I found this regex in this group: $value =~ s/\<+\>//g;
11
1666
by: David | last post by:
Hi, I'm trying to pass a querystring with certain common words removed (and, the, if, of etc). The code below replaces the keywords with "" or whatever I choose, but what I'd like to do is remove the words completely from the querystring. How do I remove rather than replace? Thanks *********
3
1103
by: anand | last post by:
Hello Group, i am stuck up to a problem, i made a search program on my web site and highlighting the searched phrase on HTML pages . well the problem is when user searches word "table" the Page also displayes highlighted "<table>" along with other page contents is there any way i can skip highlighting html, if i remove HTML tags the page is not displayed properly as i amd dumping HTML source from a table field.So finally my concern is on...
4
4185
by: Spondishy | last post by:
Hi, I'm looking for help with a regular expression and c#. I want to remove all tags from a piece of html except the following. <a> <b> <h1> <h2>
3
2435
by: Jason | last post by:
First things first, let me say that I couldn't decide whether to post this to the PHP ng, or to an XML ng. I know from experience that you guys know what you're talking about, though, and all of the questions mean "how to do this in PHP," so I hope I picked the right one ;-) For about a year, I've been importing Yahoo News headlines into my site via their RSS feed. But I would much rather import Google News headlines because I can make...
0
9800
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10778
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10439
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9597
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7990
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5817
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4642
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4244
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.