本文介绍了使用BeautifulSoup获取日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Python的新手.我正在尝试抓取一个网站以获取信息,主要是文本,但是我在日期方面遇到了问题.看起来像这样:
I'm very new to Python. I'm attempting to scrape a website for information, mostly text, but I have encountered a problem with the date. It looks like this:
<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".我的问题是它不是文本.谁能帮我.
What I want from this is "2015-04-02 14:30:12". My problem is its not text. Could anyone help me.
谢谢!
推荐答案
>>> 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
这篇关于使用BeautifulSoup获取日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!