title: How to retrieve list of string dates with BeautifulSoup Python
body: <p>I want some help in creating a small code using BeautifulSoup, for which I want to accomplish two things.</p>
  
  <p>First, I want to be able to retrieve only the dates found in the code below, ex. findAll <code>&lt;td&gt;02/02/2011&lt;/td&gt;</code>, then store it in a list. My problem is, that because there are several <code>td</code> tags, I don't know how to retrieve the date strings specifically.</p>
  
  <p>Second, after the list of dates is retrieved, I will then have two variables. One variable contains only one date, and the other variable contains list of all dates. For instance, <code>date_lst_single = "&lt;td&gt;01/04/2011&lt;/td&gt;"</code> and <code>date_lst_all = ["LIST OF ALL DATES"]</code>.</p>
  
  <p>Lastly, I want to be able to create some kind of Find condition for which I will use datetime dependency, but just need the logic behind it. Whatever the date found in <code>date_lst = &lt;td&gt;02/02/2011&lt;/td&gt;</code> in this case 02/02/2011, I want to find ONLY the date that comes before it. In the code below for instance, the previous date before such date is <code>&lt;td&gt;01/04/2011&lt;/td&gt;</code>, then retrieve the date and store in new variable.</p>
  
  <pre><code> &lt;td&gt;10/24/2011&lt;/td&gt;,
   &lt;td&gt;&lt;span class="match"&gt;11&lt;/span&gt; - &lt;span class="match"&gt;12&lt;/span&gt; - &lt;span class="match"&gt;17&lt;/span&gt; - 31 - 33&lt;/td&gt;,
   &lt;span class="match"&gt;11&lt;/span&gt;,
   &lt;span class="match"&gt;12&lt;/span&gt;,
   &lt;span class="match"&gt;17&lt;/span&gt;,
   &lt;td&gt;8,210&lt;/td&gt;,
   &lt;td&gt;$10.50&lt;/td&gt;,
   &lt;tr&gt;&lt;td&gt;06/15/2011&lt;/td&gt;&lt;td&gt;1 - 7 - &lt;span class="match"&gt;11&lt;/span&gt; - &lt;span class="match"&gt;12&lt;/span&gt; - &lt;span class="match"&gt;17&lt;/span&gt;&lt;/td&gt;&lt;td&gt;15,369&lt;/td&gt;&lt;td&gt;$6.50&lt;/td&gt;&lt;/tr&gt;,
   &lt;td&gt;06/15/2011&lt;/td&gt;,
   &lt;td&gt;1 - 7 - &lt;span class="match"&gt;11&lt;/span&gt; - &lt;span class="match"&gt;12&lt;/span&gt; - &lt;span class="match"&gt;17&lt;/span&gt;&lt;/td&gt;,
   &lt;span class="match"&gt;11&lt;/span&gt;,
   &lt;span class="match"&gt;12&lt;/span&gt;,
   &lt;span class="match"&gt;17&lt;/span&gt;,
   &lt;td&gt;15,369&lt;/td&gt;,
   &lt;td&gt;$6.50&lt;/td&gt;,
   &lt;tr class="alt"&gt;&lt;td&gt;01/04/2011&lt;/td&gt;&lt;td&gt;&lt;span class="match"&gt;11&lt;/span&gt; - &lt;span class="match"&gt;12&lt;/span&gt; - &lt;span class="match"&gt;20&lt;/span&gt; - 21 - 27&lt;/td&gt;&lt;td&gt;10,752&lt;/td&gt;&lt;td&gt;$15.00&lt;/td&gt;&lt;/tr&gt;,
   &lt;td&gt;01/04/2011&lt;/td&gt;,
   &lt;td&gt;&lt;span class="match"&gt;11&lt;/span&gt; - &lt;span class="match"&gt;12&lt;/span&gt; - &lt;span class="match"&gt;20&lt;/span&gt; - 21 - 27&lt;/td&gt;,
   &lt;span class="match"&gt;11&lt;/span&gt;,
   &lt;span class="match"&gt;12&lt;/span&gt;,
   &lt;span class="match"&gt;20&lt;/span&gt;,
   &lt;td&gt;10,752&lt;/td&gt;,
   &lt;td&gt;$15.00&lt;/td&gt;,
   &lt;tr&gt;&lt;td&gt;09/24/2009&lt;/td&gt;&lt;td&gt;2 - 3 - &lt;span class="match"&gt;11&lt;/span&gt; - &lt;span class="match"&gt;12&lt;/span&gt; - &lt;span class="match"&gt;17&lt;/span&gt;&lt;/td&gt;&lt;td&gt;11,406&lt;/td&gt;&lt;td&gt;$7.50&lt;/td&gt;&lt;/tr&gt;,
   &lt;td&gt;09/24/2009&lt;/td&gt;,
   &lt;td&gt;2 - 3 - &lt;span class="match"&gt;11&lt;/span&gt; - &lt;span class="match"&gt;12&lt;/span&gt; - &lt;span class="match"&gt;17&lt;/span&gt;&lt;/td&gt;,
   &lt;span class="match"&gt;11&lt;/span&gt;,
   &lt;span class="match"&gt;12&lt;/span&gt;,
   &lt;span class="match"&gt;17&lt;/span&gt;,
   &lt;td&gt;11,406&lt;/td&gt;,
   &lt;td&gt;$7.50&lt;/td&gt;,
   &lt;tr class="alt"&gt;&lt;td&gt;08/08/2009&lt;/td&gt;&lt;td&gt;&lt;span class="match"&gt;12&lt;/span&gt; - &lt;span class="match"&gt;20&lt;/span&gt; - 26 - 28 - &lt;span class="match"&gt;30&lt;/span&gt;&lt;/td&gt;&lt;td&gt;10,267&lt;/td&gt;&lt;td&gt;$11.00&lt;/td&gt;&lt;/tr&gt;,
   &lt;td&gt;08/08/2009&lt;/td&gt;,
   &lt;td&gt;&lt;span class="match"&gt;12&lt;/span&gt; - &lt;span class="match"&gt;20&lt;/span&gt; - 26 - 28 - &lt;span class="match"&gt;30&lt;/span&gt;&lt;/td&gt;,
   &lt;span class="match"&gt;12&lt;/span&gt;,
   &lt;span class="match"&gt;20&lt;/span&gt;,
   &lt;span class="match"&gt;30&lt;/span&gt;,
   &lt;td&gt;10,267&lt;/td&gt;,
   &lt;td&gt;$11.00&lt;/td&gt;,
   &lt;tr&gt;&lt;td&gt;05/05/2009&lt;/td&gt;&lt;td&gt;8 - &lt;span class="match"&gt;11&lt;/span&gt; - &lt;span class="match"&gt;12&lt;/span&gt; - &lt;span class="match"&gt;20&lt;/span&gt; - 26&lt;/td&gt;&lt;td&gt;11,260&lt;/td&gt;&lt;td&gt;$8.00&lt;/td&gt;&lt;/tr&gt;,
   &lt;td&gt;05/05/2009&lt;/td&gt;,
   &lt;td&gt;8 - &lt;span class="match"&gt;11&lt;/span&gt; - &lt;span class="match"&gt;12&lt;/span&gt; - &lt;span class="match"&gt;20&lt;/span&gt; - 26&lt;/td&gt;,
   &lt;span class="match"&gt;11&lt;/span&gt;,
   &lt;span class="match"&gt;12&lt;/span&gt;,
   &lt;span class="match"&gt;20&lt;/span&gt;,
   &lt;td&gt;11,260&lt;/td&gt;,
   &lt;td&gt;$8.00&lt;/td&gt;,
   &lt;tr class="alt"&gt;&lt;td&gt;04/07/2009&lt;/td&gt;&lt;td&gt;10 - &lt;span class="match"&gt;11&lt;/span&gt; - &lt;span class="match"&gt;12&lt;/span&gt; - 16 - &lt;span class="match"&gt;17&lt;/span&gt;&lt;/td&gt;&lt;td&gt;11,163&lt;/td&gt;&lt;td&gt;$8.50&lt;/td&gt;&lt;/tr&gt;,
   &lt;td&gt;04/07/2009&lt;/td&gt;,
   &lt;td&gt;10 - &lt;span class="match"&gt;11&lt;/span&gt; - &lt;span class="match"&gt;12&lt;/span&gt; - 16 - &lt;span class="match"&gt;17&lt;/span&gt;&lt;/td&gt;,
   &lt;span class="match"&gt;11&lt;/span&gt;,
   &lt;span class="match"&gt;12&lt;/span&gt;,
   &lt;span class="match"&gt;17&lt;/span&gt;,
   &lt;td&gt;11,163&lt;/td&gt;,
   &lt;td&gt;$8.50&lt;/td&gt;,
   &lt;tr&gt;&lt;td&gt;01/31/2009&lt;/td&gt;&lt;td&gt;3 - &lt;span class="match"&gt;17&lt;/span&gt; - &lt;span class="match"&gt;20&lt;/span&gt; - &lt;span class="match"&gt;30&lt;/span&gt; - 34&lt;/td&gt;&lt;td&gt;10,086&lt;/td&gt;&lt;td&gt;$11.50&lt;/td&gt;&lt;/tr&gt;,
   &lt;td&gt;01/31/2009&lt;/td&gt;,
   &lt;td&gt;3 - &lt;span class="match"&gt;17&lt;/span&gt; - &lt;span class="match"&gt;20&lt;/span&gt; - &lt;span class="match"&gt;30&lt;/span&gt; - 34&lt;/td&gt;,
   &lt;span class="match"&gt;17&lt;/span&gt;,
   &lt;span class="match"&gt;20&lt;/span&gt;,
   &lt;span class="match"&gt;30&lt;/span&gt;,
   &lt;td&gt;10,086&lt;/td&gt;,
   &lt;td&gt;$11.50&lt;/td&gt;,
   &lt;tr class="alt"&gt;&lt;td&gt;08/06/2008&lt;/td&gt;&lt;td&gt;4 - &lt;span class="match"&gt;11&lt;/span&gt; - &lt;span class="match"&gt;12&lt;/span&gt; - &lt;span class="match"&gt;30&lt;/span&gt; - 32&lt;/td&gt;&lt;td&gt;9,497&lt;/td&gt;&lt;td&gt;$11.00&lt;/td&gt;&lt;/tr&gt;,
   &lt;td&gt;08/06/2008&lt;/td&gt;,
   &lt;td&gt;4 - &lt;span class="match"&gt;11&lt;/span&gt; - &lt;span class="match"&gt;12&lt;/span&gt; - &lt;span class="match"&gt;30&lt;/span&gt; - 32&lt;/td&gt;,
   &lt;span class="match"&gt;11&lt;/span&gt;,
   &lt;span class="match"&gt;12&lt;/span&gt;,
   &lt;span class="match"&gt;30&lt;/span&gt;,
   &lt;td&gt;9,497&lt;/td&gt;,
   &lt;td&gt;$11.00&lt;/td&gt;,
  </code></pre>
  
  <p>UPDATE:</p>
  
  <p>In case this may help someone else, the last part of the code I had requested is as follow. I joined with the help I received on this post.</p>
  
  <pre><code>#Input date
  five_to_date = ['09/01/2016']
  def isDate(tag):
      if tag.name=='td':
          return bool(re.match('\d\d\/\d\d\/\d\d', tag.text))
      else:
          return False
  
  #store in list
  tds_three_five = three_five.find_all(isDate)
  dates_three = [p.text for p in tds_three_five]
  #target date
  dates_three_cnv = [datetime.strptime(x, '%m/%d/%Y').date() for x in 
  dates_three]
  
  for i in range(1):
      _lst_all_threefive = []
      for j in dates_three_cnv:
          if j &lt; five_to_date:
              _lst_all_threefive.append(j)
      three_five_to_calc = _lst_all_threefive[0] # we only want one date
  #Perform calculation of input date minus target date.
  delta = five_to_date - three_five_to_calc
  </code></pre>