Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch date with BeautifulSoup

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>

What I want from this is "2015-04-02 14:30:12". My problem is its not text. Could anyone help me.

Thanks!

like image 451
FredrikKopsch Avatar asked Dec 15 '22 15:12

FredrikKopsch


1 Answers

>>> 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
like image 192
Avinash Raj Avatar answered Dec 17 '22 06:12

Avinash Raj