Skip to main content
 首页 » 编程设计

python之使用 BeautifulSoup 获取日期

2025年05月04日13lori

我是 Python 的新手。我正在尝试抓取一个网站以获取信息,主要是文本,但我遇到了日期问题。它看起来像这样:

<time class="jlist_date_image" datetime="2015-04-02 14:30:12">Idag <span class="list_date">14:30</span></time> 

我想要的是“2015-04-02 14:30:12”。我的问题是它不是文本。谁能帮帮我。

谢谢!

请您参考如下方法:

>>> from bs4 import BeautifulSoup 
>>> s = '''<time class="jlist_date_image" datetime="2015-04-02 14:30:12">Idag <span class="list_date">14:30</span></time>''' 
>>> soup = BeautifulSoup(s) 
>>> for i in soup.findAll('time'): 
        if i.has_attr('datetime'): 
            print(i['datetime']) 
 
 
2015-04-02 14:30:12