首页 > 解决方案 > 如何从标签“澳大利亚”中获取“澳大利亚”

问题描述

我正试图从这个标签中抓住澳大利亚

<tr>
<td>City</td>
<th>Sydney</th>
</tr>
<tr>
<td>Country</td>
<th>Australia</th>
</tr>

import re
from re import findall
a = '<tr>\n<td>Country</td>\n<th>Australia</th>\n</tr>'
country = re.findall(r'<tr><td>Country</td><th>(.*?)</th></tr>',a)
print country

result: []

这是一个 html 代码,我尝试使用 import re 和 from re import findall 来抓取澳大利亚

我期待结果:澳大利亚,但它给了我结果:[]

我不想用beautifulsoup。谢谢

标签: python

解决方案


您刚刚错过了正则表达式中的换行符 (\n):

pattern = '<tr>\\n<td>Country</td>\\n<th>(.*?)</th>\\n</tr>'

这是经过测试的正则表达式


推荐阅读