Regex 使用BeuatifulSoup搜索字符串
我希望下面的代码Regex 使用BeuatifulSoup搜索字符串,regex,python-2.7,beautifulsoup,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"The Dormouse's story", u"The Dormouse's story"] 文档
from bs4 import BeautifulSoup
import re
sopa = BeautifulSoup('<ol><li>this is a string</li></ol>')
sopa.find_all(string=re.compile("a"))
当表演《爱丽丝梦游仙境》选段的汤时,它应该会回来
[u"The Dormouse's story", u"The Dormouse's story"]
文档中还说bs4使用
re.match()
(匹配字符串的开头),但其他地方(以及官方文档中的示例)指示re.search()
,并将regex“a”
更改为“this”
在任何情况下都不会对结果产生影响。如果您打印最后一行,它也可以工作,我已经试过了,它可以工作了
print(sopa.find_all(string=re.compile("a")))
您还可以通过以下方式获取文本:
print(sopa.getText())
第三种方法
print(sopa.get_text())
解决了的。版本
print(sopa.get_text())