python - 新手 Python 正则表达式问题 : Pulling dates from webpage

标签 python regex

我希望使用 Python 从网页中提取常规文本字符串 - 源代码运行如下:

<br /><strong>Date: 06/12/2010</strong> <br />

它总是开始

<strong>Date: 

&结束

</strong>

我已经抓取了网页的文本,只想提取日期和类似结构的信息。有什么建议如何做到这一点? (抱歉,这是一个新手问题!)

最佳答案

您可以使用正则表达式:

import re
pattern = re.compile(r'<strong>Date:(?P<date>.*?)</strong>') # re.MULTILINE?
# Then use it with
pattern.findall(text) # Returns all matches
# or
match = pattern.search(text) # grabs the first match
match.groupdict() # gives a dictionary with key 'date'
# or
match.groups()[0] # gives you just the text of the match.

或者尝试用 beautiful soup 来解析这个东西.

This是测试 Python 正则表达式的好地方。

关于python - 新手 Python 正则表达式问题 : Pulling dates from webpage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4462784/

相关文章:

php - 如何取代小组赛,同时保持其他一切不变?

javascript - 正则表达式在谷歌浏览器中不起作用

python - 检查是否不在列表中 - Python 中的更多条件

python - Scrapy Pipeline不向MySQL插入数据

正则表达式查找每隔一个换行符(只匹配换行符)

regex - 解析多个名称 - 正则表达式中间的 Lookbehind 不起作用

JavaScript 值在变量中未正确评估,但作为字符串正常工作

python - 无法在列表理解中使用 *= python 运算符

python - Django 中的全日历

Python OOP 实例和类的可变性