FredrikKopsch
FredrikKopsch

Reputation: 73

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!

Upvotes: 4

Views: 15188

Answers (2)

mmachine
mmachine

Reputation: 926

from bs4 import BeautifulSoup

BeautifulSoup(strng).time.attrs['datetime']

Upvotes: 6

Avinash Raj
Avinash Raj

Reputation: 174696

>>> 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

Upvotes: 9

Related Questions