-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
96 lines (81 loc) · 3.43 KB
/
functions.py
File metadata and controls
96 lines (81 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#############################################
#############################################
#
def writeFile(objectName):
with open('/Users/simon.walne/work/sustainability/'+ objectName + '.csv', "wb") as f:
writer = csv.writer(f)
writer.writerows(eval(objectName))
def getCrimeSource(postCode):
with contextlib.closing(webdriver.Firefox()) as driver:
driver.get('http://www.ukcrimestats.com/')
wait = ui.WebDriverWait(driver,10)
driver.find_element_by_id('globalsearch').click()
driver.find_element_by_id('globalsearch').send_keys(postCode)
driver.find_element_by_xpath('//*[@id="index_searchbox"]/form/table/tbody/tr[1]/td[2]/input').click()
wait.until(lambda driver: driver.find_element_by_id('tablediv'))
source = (driver.page_source).encode('utf-8')
return source;
def getHeader(headTag):
trTags = headTag.findAll("tr")
for trTag in trTags:
rowTags = trTag.findAll("th")
row = [str(rowTag.contents) for rowTag in rowTags]
row = [element.replace(r'<b>', '').replace(r'</b>', '').replace(r'[', '').replace(r']', '') for element in row]
return row;
def getBodyer(bodyTag):
trTags = bodyTag.findAll("tr")
body = []
for trTag in trTags:
tdTags = trTag.findAll("td")
row = [tdTag.contents[0] for tdTag in tdTags]
body.append(row)
return body;
def getCrimeTable(source, postCode):
"This function does some stuff"
soup = BeautifulSoup(source)
divTag = soup.find('div', attrs = {"id":"tablediv"})
theadTag = divTag.find("thead")
head = getHeader(theadTag)
tbodyTag = divTag.find("tbody")
body = getBodyer(tbodyTag)
all = [head] + body
return all;
def getSource(postCode):
br = mechanize.Browser()
br.open('https://tfl.gov.uk/maps/bus')
for form in br.forms():
for control in form.controls:
if control.name == 'Input':
br.form = form
break
br["Input"] = postCode
response2 = br.submit()
source = response2.read()
#with contextlib.closing(webdriver.Firefox()) as driver:
# driver.get('https://tfl.gov.uk/maps/bus')
# wait = ui.WebDriverWait(driver,10)
# driver.find_element_by_xpath('//*[@id="Input"]').send_keys(postCode)
# driver.find_element_by_xpath('//*[@id="search-filter-form"]/div[2]/input').click()
# wait.until(lambda driver: driver.find_element_by_id('nearby-list-container'))
# source = (driver.page_source).encode('utf-8')
#print('End of getSource')
return source;
def getStopTable(source, postCode):
"This function does some stuff"
soup = BeautifulSoup(source)
stopTag = soup.find('ul', attrs={"class" : "nearby-list"})
stopLat = stopTag["data-search-lat"]
stopLon = stopTag["data-search-lon"]
stopTable = [postCode, float(stopLat), float(stopLon)]
return stopTable;
def getBusTable(source, postCode):
"This function does some stuff"
soup = BeautifulSoup(source)
nearbyListResultTags = soup.findAll('li', attrs={"class" : "nearby-list-result"})
destinationTable = []
for result in nearbyListResultTags:
nearbyListHeadingTags = result.findAll('span', attrs = {"class":"nearby-list-heading"})
for heading in nearbyListHeadingTags:
stopName = heading.text
destinationTable.append([postCode, stopName, float(result['data-lon']), float(result['data-lat'])])
return destinationTable;