Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 760
bombay-batata Avatar asked Apr 08 '26 10:04

bombay-batata


1 Answers

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.

like image 164
Martijn Pieters Avatar answered Apr 10 '26 00:04

Martijn Pieters