-3

让我首先展示我将要处理的 3 种不同类型的字符串:

"<h1>Money Shake</h1><p>Money<br>Money<br>MORE MONEY</p><p>Take money and stuff in blender.</p><p>Blend.</p>"

"<h1>Money Shake</h1><p>Posted by Gordon Gekko</p><p>Money<br>Money<br>MORE MONEY</p><p>Take money and stuff in blender.</p><p>Blend.</p>"

"<h1>Money Shake</h1><p>Posted by Gordon Gekko</p><p>They're great</p><p>Yield: KA-CHING</p><p>Money<br>Money<br>MORE MONEY</p><p>Take money and stuff in blender.</p><p>Blend.</p>"

本质上,我想做的是撕掉含有成分的块:

"<p>Money<br>Money<br>MORE MONEY</p>"

这是我正在使用的正则表达式:

re.search(r'<p>[^</p>](.*)<br>(.*?)</p>', string, re.I)

当我在第一个和第二个字符串上使用它时,它完全符合我的要求并返回这个匹配对象:

"<p>Money<br>Money<br>MORE MONEY</p>"

但是当我在第三个字符串上使用它时,它会返回这个匹配对象:

"<p>They're great</p><p>Yield: KA-CHING</p><p>Money<br>Money<br>MORE MONEY</p>"

我在搞什么鬼?


@搅拌机

嗨 Blender,这就是我在抓取我想要的块时想出的。我确信有更好的方法,但考虑到我已经进入 Python / 编程 2 周:

def get_ingredients(soup):
   for p in soup.find_all('p'):
       if p.find('br'):
           return p

ingredients = get_ingredients(soup)

p_list = soup.find_all('p')

ingredient_index = p_list.index(ingredients)

junk = []

junk += p_list[:ingredient_index]

instructions = []

instructions += p_list[ingredient_index+1:]
4

1 回答 1

3

只需使用适当的 HTML 解析器。它会比正则表达式更直观,并且可以实际工作:

# May need to install it:
# pip install BeautifulSoup4

from bs4 import BeautifulSoup

soup = BeautifulSoup("""
    <h1>Money Shake</h1>
    <p>Posted by Gordon Gekko</p>
    <p>They're great</p>
    <p>Yield: KA-CHING</p>
    <p>
        Money
        <br>
        Money
        <br>
        MORE MONEY
    </p>
    <p>Take money and stuff in blender.</p>
    <p>Blend.</p>
""")

def get_ingredients(soup):
    for p in soup.find_all('p'):
        if p.find('br'):
            return p.find_all(text=True)
于 2013-08-21T16:38:30.457 回答