我希望使用 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/