python - 用正则表达式提取子字符串,总是没有 re.match()
问题描述
我想通过正则表达式从字符串中提取一些信息,但结果总是无。源代码如下:
line = '<meta content=\"Allrecipes\" property=\"og:site_name\"/>'
x = re.match(r'property=".+?"',line)
print(x)
我想提取内容和属性元组,我该如何解决?
解决方案
我会建议一些更合适的东西。
from bs4 import BeautifulSoup
line = '<meta content=\"Allrecipes\" property=\"og:site_name\"/>'
soup = BeautifulSoup(line, 'lxml')
print("Content: {}".format(soup.meta["content"]))
print("Property: {}".format(soup.meta["property"]))
输出:
Content: Allrecipes
Property: og:site_name
推荐阅读
- php - 如何检测 CLI PHP 进程/窗口是否使用“X”按钮关闭?
- python - 如何使用 Graph API 永久删除电子邮件?
- javascript - 有没有办法以 HTML 格式请求访问计算机上的本地存储?
- angular - 使用 akita-ng-fire 时如何避免“this.fireAuth.auth.createUserWithEmailAndPassword 不是函数”
- reactjs - 我已经成功启动了 npm,但是现在当我尝试点击 npm start 时,我发现下面提到的错误:
- node.js - 如何在我的 Google 操作中使用使用 Oauth 操场获得的刷新和访问令牌来调用 People API?
- rust - 如何使用 zbus 列出块设备?
- java - Maven构建依赖问题
- react-native - 如何在场景加载时禁用 iOS 反弹效果?
- javascript - 在 asp.net 网络表单中使用 Sweet 警报删除行