使用BeuatifulSoup

时间:2016-01-27 04:16:35

标签: regex python-2.7 beautifulsoup

我期待以下代码

from bs4 import BeautifulSoup
import re
sopa = BeautifulSoup('<ol><li>this is a string</li></ol>')
sopa.find_all(string=re.compile("a"))

会返回[u'this is a string'],但会返回[]。为什么呢?

BeautifulSoup 4文档给出了这个例子:

soup.find_all(string=re.compile("Dormouse"))

当作为爱丽丝梦游仙境节目的汤时,应该返回

[u"The Dormouse's story", u"The Dormouse's story"]

文档还说bs4使用re.match()(匹配字符串的开头),但其他地方(以及官方文档中的此示例)表示re.search(),并更改正则表达式{在任何情况下,{1}} "a"对结果都没有影响。

1 个答案:

答案 0 :(得分:0)

如果你打印最后一行,我已经尝试了,它的工作

也可以
print(sopa.find_all(string=re.compile("a")))

您也可以通过

获取文字
print(sopa.getText())

和第三种方法

print(sopa.get_text())
相关问题