regex - 正则表达式错误
问题描述
我正在尝试从以下短语中提取国家(此处为印度尼西亚):
<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()
仅提取印度尼西亚的正确正则表达式命令是什么?
解决方案
也许,从bs4
importing 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
推荐阅读
- mongodb - 如何从另一个 Windows 服务器连接 mongodb
- .net - .NET Core xUnit 项目无法识别 app.config 或 appsettings.json
- mysql - Xampp 不会连接到 MYsql 数据库或 ProFTPD
- c - 通过终端将文本文件的内容传递给 main 时获取(null)
- unicode - 我怎样才能输入这个特殊字符?
- xslt - XSLT 2.0 基于自闭合元素之间的内容输出 html 列表
- django - Django:需要手动渲染表单选项
- css - 防止 Safari iPad 上的弹性滚动
- c# - .net mvc 5 oauth2 服务器
- node.js - 无法编译构建