Create a string array of all comments in a html file...

S

sophie_newbie

Hi, I'm wondering how i'd go about extracting a string array of all
comments in a HTML file, HTML comments obviously taking the format
"<!-- Comment text here -->".

I'm fairly stumped on how to do this? Maybe using regular expressions?

Thanks.
 
W

William James

Hi, I'm wondering how i'd go about extracting a string array of all
comments in a HTML file, HTML comments obviously taking the format
"<!-- Comment text here -->".

I'm fairly stumped on how to do this? Maybe using regular expressions?

Thanks.

E:\Ruby>irb --prompt xmp
"<!-- Comment
here -->And <i>so</i> funny!
<p>It was a dark and stormy night.
</p><!-- Comment <> -->".scan(/<!--.*?-->/m)
==>["<!-- Comment\nhere -->", "<!-- Comment <> -->"]
 
P

Paul McGuire

Hi, I'm wondering how i'd go about extracting a string array of all
comments in a HTML file, HTML comments obviously taking the format
"<!-- Comment text here -->".

I'm fairly stumped on how to do this? Maybe using regular expressions?

Thanks.
.... here -->And <i>so</i> funny!
.... </p><!-- Comment <> -->""").asList()
[['<!-- Comment \nhere -->'], ['<!-- Comment <> -->']]

-- Paul
 
S

Stefan Behnel

sophie_newbie said:
Hi, I'm wondering how i'd go about extracting a string array of all
comments in a HTML file, HTML comments obviously taking the format
"<!-- Comment text here -->".

I'm fairly stumped on how to do this? Maybe using regular expressions?


from lxml import etree

parser = etree.HTMLParser()
tree = etree.parse("somefile.html", parser)

print tree.xpath("//comment()")


http://codespeak.net/lxml

Stefan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top