2017-04-16 63 views
-3

如何在不使用Python 3的美丽汤的情况下获得标记之间的html值? 我想列出标签之间的值。 我该怎么做?我会使用正则表达式吗?如何从使用Python 3的HTML标记中获取值?

<td class="standing-table__cell standing-table__cell--name">Chelsea</td> 
<td class="standing-table__cell standing-table__cell--name">Tottenham</td> 
<td class="standing-table__cell standing-table__cell--name">Liverpool</td> 

我将如何获得'切尔西','托特纳姆','利物浦'这样的值?

感谢

+1

谷歌的第一个结果,https://docs.python.org/3/library/html.parser.html – smoggers

+0

有没有没有使用“美丽的汤”的具体原因? –

+0

我被告知我只能使用标准库 – kiwi

回答

0

我建议BS4,但如果你想使用正则表达式:

my_str = '<td class="standing-table__cell standing-table__cell--name">Chelsea</td>' 
match = re.search('>(.*?)</', my_str) 
if match : 
    match = match.group(1) 

这将匹配HTML标签之间的任何字符串

print(match) 
Chelsea 

记住,search会返回第一个匹配(如果有的话,否则无)
如果你想要所有的事件,你应该使用findall改为

+0

如果没有匹配,会发生什么情况? –

+0

@Pedro Lobito我从来没有声称这是最好的解决方案(实际上我推荐bs4),它只是一个快速的例子。我还提到're.search'可能会返回'None',如果这就是你的意思。无论如何,我更新了代码来处理这种情况,感谢您的意见。 –

+0

太好了,你可以使用'if match:'only。 –

相关问题