python 正则获取html中的值

# -*- 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 = """
    
发表于 4 天前 |倒序浏览
"""
get_timestamp(html)

你可能感兴趣的:(python基础)