亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么在使用的時候不能匹配到時間?如何解決?謝謝!

為什么在使用的時候不能匹配到時間?如何解決?謝謝!

紅糖糍粑 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 關注
  • 143 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號