为了账号安全,请及时绑定邮箱和手机立即绑定

为什么在使用的时候不能匹配到时间?如何解决?谢谢!

为什么在使用的时候不能匹配到时间?如何解决?谢谢!

红糖糍粑 2023-05-01 11:07:27
想做一个爬虫,使用BeautifulSoup假使获取以下内容:<div class="authi"><img class="authicn vm" id="" src="static/image/common/online_member.gif" /><em id="">发表于 <span title="2013-4-2 08:30:11">4&nbsp;天前</span></em><span class="pipe">|</span><a href=>倒序浏览</a><div style="float:right;position:relative;top:-6px;"><ul><li style="float: left; width: 130px; height: 24px;"><wb:follow-button uid="" type="gray_2" width="136" height="24"></wb:follow-button></li><li style=" float:left; width: 160px; height: 24px;"></li></ul></div></div>如何获取以上内容中的2013-4-2 08:30:11这个时间呢?我试着写了一个re.compile('^20\d{2}-\d+-\d+\s\d{2}:\d{2}:\d{2}')
查看完整描述

2 回答

?
弑天下

TA贡献1818条经验 获得超8个赞

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


查看完整回答
反对 回复 2023-05-03
?
www说

TA贡献1775条经验 获得超8个赞

# -*- 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 = """    <div class="authi">
    <img class="authicn vm" id="" src="static/image/common/online_member.gif" />
    <em id="">发表于 <span title="2013-4-2 08:30:11">4&nbsp;天前</span></em>
    <span class="pipe">|</span><a href=>倒序浏览</a>
    <div style="float:right;position:relative;top:-6px;"><ul>
    <li style="float: left; width: 130px; height: 24px;"><wb:follow-button uid="" type="gray_2" width="136" height="24"></wb:follow-button></li>
    <li style=" float:left; width: 160px; height: 24px;"></li>
    </ul></div></div>
    """

    get_timestamp(html)
查看完整回答
反对 回复 2023-05-03
  • 2 回答
  • 0 关注
  • 119 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信