title: Scraping values from HTML header and saving as a CSV file in Python
body: <p>All,</p>
  
  <p>I've just started using Python (v 2.7.1) and one of my first programs is trying to scrape information from a website containing power station data using the Standard Library and BeautifulSoup to handle the HTML elements.</p>
  
  <p>The data I'd like to access is obtainable in either the 'Head' section of the HTML or as tables within the main body. The website will generate a CSV file from it data if the CSV link is clicked.</p>
  
  <p>Using a couple of sources on this website I've managed to cobble together the code below which will pull the data out and save it to a file, but, it contains the \n designators. Try as I might, I can't get a correct CSV file to save out.</p>
  
  <p>I am sure it's something simple but need a bit of help if possible!</p>
  
  <pre><code>from BeautifulSoup import BeautifulSoup
  
  import urllib2,string,csv,sys,os
  from string import replace
  
  bm_url = 'http://www.bmreports.com/servlet/com.logica.neta.bwp_PanBMDataServlet?param1=T_COTPS-4&amp;param2=&amp;param3=&amp;param4=&amp;param5=2011-02-05&amp;param6=*'
  
  data = urllib2.urlopen(bm_url).read()
  soup = BeautifulSoup(data)
  data = str(soup.findAll('head',limit=1))
  
  data = replace(data,'[&lt;head&gt;','')
  data = replace(data,'&lt;script language="JavaScript" src="/bwx_generic.js"&gt;&lt;/script&gt;','')
  data = replace(data,'&lt;link rel="stylesheet" type="text/css" href="/bwx_style.css" /&gt;','')
  data = replace(data,'&lt;title&gt;Historic Physical Balancing Mechanism Data&lt;/title&gt;','')
  data = replace(data,'&lt;script language="JavaScript"&gt;','')
  data = replace(data,' &lt;/script&gt;','')
  data = replace(data,'&lt;/head&gt;]','')
  data = replace(data,'var gs_csv=','')
  data = replace(data,'"','')
  data = replace(data,"'",'')
  data = data.strip()
  
  file_location = 'c:/temp/'
  file_name = file_location + 'DataExtract.txt'
  
  file = open(file_name,"wb")
  file.write(data)
  file.close()
  </code></pre>