import pyperclip
import re
import time

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

import postcodes


def care_costs_for_postcode(postcode):
    browser = webdriver.Firefox()
    actions = ActionChains(browser)
    browser.get('http://www.bbc.co.uk/news/health-30990913')

    inner_frame = browser.find_elements_by_xpath('//iframe')[1]
    inner_frame.send_keys(Keys.ENTER)
    inner_frame.send_keys(Keys.TAB)
    inner_frame.send_keys(Keys.ENTER)
    inner_frame.send_keys(Keys.TAB)
    inner_frame.send_keys(Keys.TAB)
    inner_frame.send_keys(postcode)
    inner_frame.send_keys(Keys.ENTER)

    time.sleep(2)

    actions.send_keys(Keys.CONTROL, 'f')
    actions.send_keys(Keys.CONTROL, 'weekly average rate')
    actions.send_keys(Keys.ESCAPE).perform()

    actions.move_to_element(inner_frame)
    actions.click()
    actions.perform()

    time.sleep(2)

    for i in range(100):
        actions.send_keys(Keys.SHIFT, Keys.ARROW_RIGHT)

    actions.perform()

    actions.send_keys(Keys.CONTROL, 'c').perform()
    browser.quit()

    copied_text = pyperclip.paste()
    occurrences = re.findall('[0-9\.]+', copied_text)
    if len(occurrences) == 2:
        residential, at_home = occurrences
        return residential, at_home
    return '?', '?'


def main():
    dist_posts = postcodes.district_postcodes('postcodes.grouped.csv')
    for district, postcode in dist_posts.itertuples():
        residential_care, at_home_care = care_costs_for_postcode(postcode)
        print 'District,Postcode,Residential care, At home care'
        print ','.join((district, postcode, residential_care, at_home_care))


if __name__ == '__main__':
    main()

@@ -1,16 +1,36 @@
+import pandas as pd%0A
 import pyperclip
@@ -30,16 +30,30 @@
 perclip%0A
+import random%0A
 import r
@@ -216,25 +216,25 @@
 ys%0A%0A
-import postcodes%0A
+%0Arandom.seed(101)
 %0A%0Ade
@@ -1153,28 +1153,8 @@
 m()%0A
-    browser.quit()%0A%0A
     
@@ -1184,16 +1184,42 @@
 .paste()
+ or ''%0A    browser.quit()%0A
 %0A    occ
@@ -1378,129 +1378,396 @@
 ome%0A
-    return '?', '?'%0A%0A%0Adef main():%0A    dist_posts = postcodes.district_postcodes('postcodes.grouped.csv')
+%0A%0Adef randomised(ol):%0A    random.shuffle(ol)%0A    return iter(ol)%0A%0A%0Adef care_costs_for_postcodes(postcodes):%0A    for p in randomised(postcodes):%0A        res = care_costs_for_postcode(p)%0A        if res:%0A            return res + (p,)%0A    return (-1, -1, p)%0A%0A%0Adef district_postcodes(filename):%0A    # read csv file%0A    df = pd.read_csv(filename, sep=',')%5B%5B'Postcode', 'District'%5D%5D
 %0A    
-for 
 district
 , po
@@ -1754,32 +1754,60 @@
 '%5D%5D%0A    district
+_to_postcodes = %7B%7D%0A    for i
 , postcode in di
@@ -1804,22 +1804,24 @@
 code
- in
+,
  dist
-_posts
+rict in df
 .ite
@@ -1843,55 +1843,144 @@
     
-residential_care, at_home_care = care_costs_for
+district_to_postcodes.setdefault(district, %5B%5D).append(postcode)%0A    return district_to_postcodes%0A%0A%0Adef main():%0A    dist_posts = district
 _pos
@@ -1984,25 +1984,27 @@
 postcode
-(
+s('
 postcode
 )%0A      
@@ -1995,26 +1995,38 @@
 postcode
+s.simple.ew.csv'
 )%0A    
- 
-   
 print 'D
@@ -2059,17 +2059,16 @@
 al care,
- 
 At home 
@@ -2073,16 +2073,148 @@
 e care'%0A
+    for district, postcodes in dist_posts.iteritems():%0A        residential, at_home, postcode = care_costs_for_postcodes(postcodes)%0A
         
@@ -2260,21 +2260,16 @@
 idential
-_care
 , at_hom
@@ -2273,13 +2273,8 @@
 home
-_care
 ))%0A%0A

Version to get all English postcodes (fails with Welsh and Scotish)