title: Turn an HTML table into a CSV file
body: <p>How do I turn a table like this--<a href="https://www.baseball-reference.com/players/gl.fcgi?id=abreuto01&amp;t=b&amp;year=2010" rel="nofollow noreferrer">batting gamelogs table</a>--into a CSV file using Python and BeautifulSoup?</p>
  
  <p>I want the first header where it says Rk, Gcar, Gtm, etc. and not any of the other headers within the table (the ones for each month of the season).</p>
  
  <p>Here is the code I have so far:</p>
  
  <pre><code>from bs4 import BeautifulSoup
  from urllib2 import urlopen
  import csv
  
  def stir_the_soup():
      player_links = open('player_links.txt', 'r')
      player_ID_nums = open('player_ID_nums.txt', 'r')
      id_nums = [x.rstrip('\n') for x in player_ID_nums]
      idx = 0
      for url in player_links:
          print url
          soup = BeautifulSoup(urlopen(url), "lxml")
          p_type = ""
          if url[-12] == 'p':
              p_type = "pitching"
          elif url[-12] == 'b':
              p_type = "batting" 
          table = soup.find(lambda tag: tag.name=='table' and tag.has_attr('id') and tag['id']== (p_type + "_gamelogs"))
          header = [[val.text.encode('utf8') for val in table.find_all('thead')]]
          rows = []
          for row in table.find_all('tr'):
              rows.append([val.text.encode('utf8') for val in row.find_all('th')])
              rows.append([val.text.encode('utf8') for val in row.find_all('td')])
          with open("%s.csv" % id_nums[idx], 'wb') as f:
              writer = csv.writer(f)
              writer.writerow(header)
              writer.writerows(row for row in rows if row)
          idx += 1
      player_links.close()
  
  if __name__ == "__main__":
      stir_the_soup()
  </code></pre>
  
  <p>The id_nums list contains all of the id numbers for each player to use as the names for the separate CSV files.</p>
  
  <p>For each row, the leftmost cell is a  tag and the rest of the row is  tags. In addition to the header how do I put that into one row?</p>