BeautifulSoup - 获取无HTML内容的简便方法

And*_*mbu 7 python beautifulsoup html-parsing html-content-extraction

我正在使用此代码查找页面中所有有趣的链接:

soup.findAll('a', href=re.compile('^notizia.php\?idn=\d+'))
Run Code Online (Sandbox Code Playgroud)

它的工作做得很好.不幸的是,标签里面有很多嵌套标签,比如字体,b和不同的东西......我只想得到文本内容,没有任何其他的html标签.

链接示例:

<A HREF="notizia.php?idn=1134" OnMouseOver="verde();" OnMouseOut="blu();"><FONT CLASS="v12"><B>03-11-2009:&nbsp;&nbsp;<font color=green>CCS Ingegneria Elettronica-Sportello studenti ed orientamento</B></FONT></A>
Run Code Online (Sandbox Code Playgroud)

当然它很难看(而且标记并不总是一样!)我想得到:

03-11-2009:  CCS Ingegneria Elettronica-Sportello studenti ed orientamento
Run Code Online (Sandbox Code Playgroud)

它说text=True在findAll方法中使用的文档,但它会忽略我的正则表达式.为什么?我怎么解决这个问题?

Jon*_*erg 12

我用过这个:

def textOf(soup):
    return u''.join(soup.findAll(text=True))
Run Code Online (Sandbox Code Playgroud)

所以...

texts = [textOf(n) for n in soup.findAll('a', href=re.compile('^notizia.php\?idn=\d+'))]
Run Code Online (Sandbox Code Playgroud)