bombay-batata
bombay-batata

Reputation: 43

Extract dates using BeautifulSoup 4

how to extract the date in this using BeautifulSoup?

<div class="month">                                            Dec                                          </div>                                             
<div class="edate">                                                 31                                             </div>                                             
<div class="day">                                                 Mon                                             </div

Upvotes: 3

Views: 1193

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121466

Take the parent element of those divs, then get the three strings and join them into one string:

date = ' '.join([unicode(t) for t in parent.stripped_strings])

which would result in Dec 31 Mon.

If you need to manipulate the date, you'll need to parse it out to a datetime.date object; I strongly suggest you use the dateutil external library to do that. However, since the year is missing from this date, your mileage may vary.

Upvotes: 4

Related Questions