daily pastebin goal
7%
SHARE
TWEET

pyq1

a guest Dec 6th, 2018 46 in 2 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # This version of the code saves the data into a list of tuples (so that
  2. # we can put them into our database table later).
  3.  
  4. data4table = []
  5.  
  6. start_row = 2
  7. pres_num = 1
  8. for row in range(2, len(table_rows)):
  9.     data_items = table_rows[row].select('td')
  10.     try:
  11.         name = data_items[3].find('a').text
  12.     except:
  13.         continue
  14.     if name == 'Donald Trump':
  15.         pass  ## ... if only.
  16.     else:
  17.         try:
  18.             start_term = data_items[1].select('span')[0].text
  19.         except:
  20.             start_term = data_items[1].contents[0]  ## FDR special case.
  21.     if 'Incumbent' in data_items[1].text:
  22.         end_year = '2017'   # Current president: no end date.
  23.     elif 'Beginning' in data_items[1].text:
  24.         end_year = '2020'   # Future president: no end date.
  25.     else:
  26.         try:
  27.             end_term = data_items[1].select('span')[1].text
  28.         except:
  29.             end_term = data_items[1].contents[4]  ## FDR special case.
  30.         end_of_term_year = end_term[-4:]
  31.         year_of_death = data_items[3].contents[2][-4:]
  32.         if year_of_death.find('–') != -1:
  33.             year_of_death = data_items[3].contents[3].text
  34.  
  35.     end_term = end_term.strip()  # A little cleanup.
  36.    
  37.     if table_rows[row].select('a')[1].text == 'Died in office':
  38.         which_anchor = 3
  39.     else:
  40.         which_anchor = 2
  41.        
  42.     pres_url = ''
  43.     for anchor in table_rows[row].select('a'):
  44.         if 'title' in anchor.attrs:
  45.             if anchor.attrs['title'] == name:
  46.                 pres_url = anchor.attrs['href']            
  47.  
  48.     more_details = BASE_URL+pres_url
  49.     birth, death = determine_pres_dates(more_details)
  50.     if len(death) == 4:  # President or ex-president still lives.
  51.         death = None
  52.  
  53.     data4table.append((name,
  54.                        scraped2mysqldate(start_term),
  55.                        scraped2mysqldate(end_term),
  56.                        more_details,
  57.                        birth,
  58.                        death))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top