如何将所有 3 合 1 re.findall() ??(python 2.7 && 正则表达式)

How to combine all 3 in 1 re.findall() ??(python 2.7 && Regular Expressions)

Filter1=re.findall(r'<span (.*?)</span>',PageSource) 
Filter2=re.findall(r'<a href=.*title="(.*?)" >',PageSource) 
Filter3=re.findall(r'<span class=.*?<b>(.*?)</b>.*?',PageSource)

如何在 1 行代码中完成...像这样:

Filter=re.findall(r'  ',PageSource)

我这样试过:

Filter=re.findall(r'<span (.*?)</span>'+
                  r'<a href=.*title="(.*?)" >'+
                  r'<span class=.*?<b>(.*?)</b>.*?',PageSource)

但它不起作用。

使用 HTML 解析器 怎么样?

示例,使用 BeautifulSoup:

from bs4 import BeautifulSoup

data = "your HTML here"
soup = BeautifulSoup(data)

span_texts = [span.text for span in soup.find_all('span')]
a_titles = [a['title'] for a in soup.find_all('a', title=True)]
b_texts = [b.text for b in soup.select('span[class] > b')]

result = span_texts + a_titles + b_texts

演示:

>>> from bs4 import BeautifulSoup
>>> 
>>> data = """
... <div>
...     <span>Span's text</span>
...     <a title="A title">link</a>
...     <span class="test"><b>B's text</b></span>
... </div>
... """
>>> soup = BeautifulSoup(data)
>>> 
>>> span_texts = [span.text for span in soup.find_all('span')]
>>> a_titles = [a['title'] for a in soup.find_all('a', title=True)]
>>> b_texts = [b.text for b in soup.select('span[class] > b')]
>>> 
>>> result = span_texts + a_titles + b_texts
>>> print result
[u"Span's text", u"B's text", 'A title', u"B's text"]

除此之外,您的正则表达式非常不同并且有不同的用途 - 我不会尝试挤压不可挤压,将它们分开并将结果组合到一个列表中。