954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how do I extract data from html file ?

Hi,
I want write a program which
extract 'Rated PG for some scary moments and mild language' from the following html file and return it as a list .

html file:

MPAA:


Rated PG for some scary moments and mild language. (also 2009 extended version)

Why wouldnt this code work ?
mpaaget = re.compile('MPAA:(.*?)')
mpaa = mpaaget.findall(htmlr)

masterinex
Newbie Poster
17 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

use a html parser for this job, such as BeautifulSoup. If you don't want to, then another way is to read the whole html, split on "

ghostdog74
Junior Poster
156 posts since Apr 2006
Reputation Points: 75
Solved Threads: 44
 
use a html parser for this job, such as BeautifulSoup. If you don't want to, then another way is to read the whole html, split on "
masterinex
Newbie Poster
17 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

ohw I got it now , thanks for pointing it out .

masterinex
Newbie Poster
17 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

hey, I tried it with mpaaget = re.compile('(.*?)') but then I got something else . Could it be because there is a new line after ? How do I take care of that? Yes, the white space does not fit into your regular expression. Modify like so to match 0 or any number (*) of white space characters (\s):

>>> m = re.compile('<h5><a href="/mpaa">MPAA</a>:</h5>\s*<div class="info-content">\s*(.*?)\s*</div>')
>>> m.findall(h)
['Rated PG for some scary moments and mild language. (also 2009 extended version)']
>>> m.match(h)
>>>
jlm699
Veteran Poster
1,112 posts since Jul 2008
Reputation Points: 355
Solved Threads: 292
 

Yea , that was problem ,thanks for pointing it out again .
Looks like \n and \s* are the same character .

I have another question. lets say
I want to extract the number 7.2 from the html string below :

weighted average vote of 7.2 / 10

how come this doesnt work ?

averageget = re.compile('(.*?)')
average = averageget.findall(htmlr)

Could it be that there some special structures in the html file again which I missed out ?

masterinex
Newbie Poster
17 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

This time it's because '?' is a special character in regular expressions (you're using it inside your group). The question mark indicates a greedy match of 1 or more (where as the asterick (*) is a greedy match of 0 or more). To match the question mark character itself you need to escape it in your regex like so: \? . The full regular expression then becomes:

>>> c = re.compile('<a href="/List\?ratings=7">(.*?)</a>')
>>> c.findall(t)
['7.2']
jlm699
Veteran Poster
1,112 posts since Jul 2008
Reputation Points: 355
Solved Threads: 292
 

ohw I see so its the '?' that causing the trouble ,
what is t btw do I need to assighn a value to it ?

masterinex
Newbie Poster
17 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

if you want to use regex, you should compile your regex with re.DOTALL and re.M for multiline match.

ghostdog74
Junior Poster
156 posts since Apr 2006
Reputation Points: 75
Solved Threads: 44
 

Im a little unfamiliar with Python , what are re.DOTALL and re.M are they modules ?

masterinex
Newbie Poster
17 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

They are Flags for compile():
re.MULTILINE (or re.M) string and each line
re.DOTALL (or re.S) match any character, including a newline
re,IGNORECASE (or re.I) case-insensitive matching

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: