要捕获HTML标签之间的字符串,可以使用正则表达式。以下是一个示例代码:
import re
def extract_string_between_tags(html):
pattern = r'<.*?>(.*?)<\/.*?>' # 匹配HTML标签之间的字符串
matches = re.findall(pattern, html)
return matches
html = 'This is a paragraph.
Example'
strings = extract_string_between_tags(html)
print(strings)
输出:
['This is a paragraph.', 'Example']
在上面的代码中,我们定义了一个extract_string_between_tags
函数,它接受一个包含HTML代码的字符串作为输入。使用正则表达式<.*?>(.*?)<\/.*?>
来匹配HTML标签之间的字符串,并使用re.findall
函数找到所有匹配的字符串。