Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 使用BeuatifulSoup搜索字符串_Regex_Python 2.7_Beautifulsoup - Fatal编程技术网

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())