当我阅读文本时,我有类似 <h3 class="heading">General Purpose</h3>
的字符串在文本的某些行中,现在我只想获得 General Purpose
的值从上面..
d = re.search(re.escape('<h3 class="heading">')+"(.*?)"+re.escape('</h3>'), str(data2))
if d:
print(d.group(0))
最佳答案
import re
text="""<h3 class="heading">General Purpose</h3>"""
pattern="(<.*?>)(.*)(<.*?>)"
g=re.search(pattern,text)
g.group(2)
输出:
'General Purpose'
如果它是一个漂亮的 soup 对象,那么获取值就更简单了。你不需要正则表达式。
from bs4 import BeautifulSoup
text="""<h3 class="heading">General Purpose</h3>"""
a=BeautifulSoup(text)
print a.select('h3.heading')[0].text
输出:
General Purpose
关于Python正则表达式获取两个字符串之间的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40602714/