在Python中查找字符串中的字符串(和之间)

时间:2013-10-13 18:03:07

标签: python regex

我有这样的文字:

<div style="margin-left:10px;margin-right:10px;">
<!-- start of lyrics -->
There are times when I've wondered<br />
And times when I've cried<br />
When my prayers they were answered<br />
At times when I've lied<br />
But if you asked me a question<br />
Would I tell you the truth<br />
Now there's something to bet on<br />
You've got nothing to lose<br />
<br />
When I've sat by the window<br />
And gazed at the rain<br />
With an ache in my heart<br />
But never feeling the pain<br />
And if you would tell me<br />
Just what my life means<br />
Walking a long road<br />
Never reaching the end<br />
<br />
God give me the answer to my life<br />
God give me the answer to my dreams<br />
God give me the answer to my prayers<br />
God give me the answer to my being
<!-- end of lyrics -->
</div>

我想打印这首歌的歌词,但re.findall和re.search在这种情况下不起作用。我如何能?我正在使用此代码:

lyrics = re.findall('<div style="margin-left:10px;margin-right:10px;">(.*?)</div>', open('file.html','r').read())   

for words in lyrics:
    print words

3 个答案:

答案 0 :(得分:1)

您不应该使用正则表达式来解析HTML。

看起来你正在抓一个网站。您可以在lxml内使用scrapyxpath

Python 2.7.5+ (default, Sep 19 2013, 13:48:49) 
>>> html = """<div style="margin-left:10px;margin-right:10px;">
... <!-- start of lyrics -->
... There are times when I've wondered<br />
... And times when I've cried<br />
... When my prayers they were answered<br />
... At times when I've lied<br />
... But if you asked me a question<br />
... Would I tell you the truth<br />
... Now there's something to bet on<br />
... You've got nothing to lose<br />
... <br />
... When I've sat by the window<br />
... And gazed at the rain<br />
... With an ache in my heart<br />
... But never feeling the pain<br />
... And if you would tell me<br />
... Just what my life means<br />
... Walking a long road<br />
... Never reaching the end<br />
... <br />
... God give me the answer to my life<br />
... God give me the answer to my dreams<br />
... God give me the answer to my prayers<br />
... God give me the answer to my being
... <!-- end of lyrics -->
... </div>"""
>>> import lxml.html
>>> html = lxml.html.fromstring(html)
>>> html.text_content()
"\n\nThere are times when I've wondered\nAnd times when I've cried\nWhen my prayers they were answered\nAt times when I've lied\nBut if you asked me a question\nWould I tell you the truth\nNow there's something to bet on\nYou've got nothing to lose\n\nWhen I've sat by the window\nAnd gazed at the rain\nWith an ache in my heart\nBut never feeling the pain\nAnd if you would tell me\nJust what my life means\nWalking a long road\nNever reaching the end\n\nGod give me the answer to my life\nGod give me the answer to my dreams\nGod give me the answer to my prayers\nGod give me the answer to my being\n\n"
>>> 

答案 1 :(得分:1)

试试这个:

with open(r'<file_path>','r') as file:
        for line in file:
            if  re.match(r'^<', line) == None:
                print line[:line.find(r'<')]

<强>输出

There are times when I've wondered
And times when I've cried
When my prayers they were answered
At times when I've lied
But if you asked me a question
Would I tell you the truth
Now there's something to bet on
You've got nothing to lose
When I've sat by the window
And gazed at the rain
With an ache in my heart
But never feeling the pain
And if you would tell me
Just what my life means
Walking a long road
Never reaching the end
God give me the answer to my life
God give me the answer to my dreams
God give me the answer to my prayers
God give me the answer to my being

修改 使用Url lib 并从网络中提取歌词:

from lxml import etree
import urllib, StringIO

# Rip file from URL        
resultado=urllib.urlopen('http://www.azlyrics.com/lyrics/ironmaiden/noprayerforthedying.html')
html = resultado.read()
# Parse html to etree
parser= etree.HTMLParser()
tree=etree.parse(StringIO.StringIO(html),parser)
# Apply the xpath rule
e = tree.xpath("//div[@style='margin-left:10px;margin-right:10px;']/text()")
# print output
for i in e:
    print str(i).strip()

答案 2 :(得分:0)

对于HTML代码的这个特定部分,我不明白为什么re.findall不起作用。 四行实际代码加上文本可以产生输出。

from re import findall

html = """
<div style="margin-left:10px;margin-right:10px;">
<!-- start of lyrics -->
There are times when I've wondered<br />
And times when I've cried<br />
When my prayers they were answered<br />
At times when I've lied<br />
But if you asked me a question<br />
Would I tell you the truth<br />
Now there's something to bet on<br />
You've got nothing to lose<br />
<br />
When I've sat by the window<br />
And gazed at the rain<br />
With an ache in my heart<br />
But never feeling the pain<br />
And if you would tell me<br />
Just what my life means<br />
Walking a long road<br />
Never reaching the end<br />
<br />
God give me the answer to my life<br />
God give me the answer to my dreams<br />
God give me the answer to my prayers<br />
God give me the answer to my being
<!-- end of lyrics -->
</div>
"""

raw = findall(r'.*<br />', html)

for line in raw:
    line = line.strip('<br />')
    print(line)