首页 > 解决方案 > 正则表达式错误

问题描述

我正在尝试从以下短语中提取国家(此处为印度尼西亚):

<small class="text-muted">
                            <span class="hidden-xs">Football / </span>Indonesia / 
                            <span class="hidden-xs xh-highlight">Kick off: </span>11 Sep 2019, 11:30                            </small>

目前,我只是使用以下命令来提取文本:

.xpath('.//small[@class="text-muted"]/text()').extract()

仅提取印度尼西亚的正确正则表达式命令是什么?

标签: regexxpathscrapyweb-crawler

解决方案


也许,从bs4importing BeautifulSoup,我们可以提取国家,如果可以的话:

from bs4 import BeautifulSoup
import re

string="""
<small class="text-muted">
                            <span class="hidden-xs">Football / </span>Indonesia / 
                            <span class="hidden-xs xh-highlight">Kick off: </span>11 Sep 2019, 11:30                            </small>
A

"""

soup = BeautifulSoup(string, 'html.parser').find_all('small')[0].text

print(re.findall(r'[^/]+/\s*([^/]+?)\s*/', soup)[0])

输出

Indonesia

推荐阅读