-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdividen.py
executable file
·63 lines (47 loc) · 1.87 KB
/
dividen.py
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
#!/bin/env python
# Script to extract list of KLSE stocks that give dividend and priced less than or equal to RM 1
# Copyright (c) 2015 Sharuzzaman Ahmat Raslan [email protected]
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import urllib2
from BeautifulSoup import BeautifulSoup
import csv
# grab the HTML from the website
response = urllib2.urlopen('http://klse.i3investor.com/jsp/divann.jsp')
rawfile = response.read()
# create beautifulsoup object
soup = BeautifulSoup(rawfile)
# grab 6th table in the HTML
tables = soup('table')[6]
# grab the table header
thead = soup('table')[6].thead
# get the text of the table header
headers = [header.text for header in thead.findAll('th')]
# remove detail column
headers.pop(0)
rows = []
# get value for all column, except column 1
for row in tables.findAll('tr')[1:]:
rows.append([val.text for val in row.findAll('td')[1:]])
# only select column 4 if the value is less than or equal to 1
data = [ x for x in rows if float(x[3]) <= 1 ]
# convert string to float in column 4
for i in data:
i[3] = float(i[3])
# create a sorted list, sorted according to column 4
sorteddata = sorted(data, key=lambda v: v[3])
# write the data into CSV file
with open('dividen.csv', 'wb') as f:
writer = csv.writer(f)
writer.writerow(headers)
writer.writerows(sorteddata)