html论坛时间正则,python使用正则获取html中的时间

想做一个爬虫,使用BeautifulSoup假使获取以下内容:

online_member.gif

发表于 4 天前

|倒序浏览

如何获取以上内容中的2013-4-2 08:30:11这个时间呢?

我试着写了一个

re.compile('^20\d{2}-\d+-\d+\s\d{2}:\d{2}:\d{2}')

可是这个在使用的时候不能匹配到时间?如何解决?谢谢!

回答:

# -*- coding: utf-8 -*-

from BeautifulSoup import BeautifulSoup

import re

def get_timestamp(html):

soup = BeautifulSoup(html)

authi_elems = soup.findAll('div', { 'class': 'authi' })

for authi_elem in authi_elems:

date_elem = authi_elem.find('span', title=re.compile(r'20\d{2}-\d+-\d+\s\d{2}:\d{2}:\d{2}'))

print date_elem.get('title')

if __name__ == '__main__':

html = """

online_member.gif

发表于 4 天前

|倒序浏览

"""

get_timestamp(html)

回答:

re.compile('20\d{2}-\d+-\d+\s\d{2}:\d{2}:\d{2}')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值