-
Notifications
You must be signed in to change notification settings - Fork 0
/
counties.py
75 lines (63 loc) · 2.18 KB
/
counties.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
64
65
66
67
68
69
70
71
72
73
74
75
# -*- coding: utf-8 -*-
"""
Created on Fri May 1 22:46:33 2020
@author: ovicko
"""
from bs4 import BeautifulSoup
import requests
import time
import xlsxwriter
hudumaUrl = "https://www.hudumanamba.go.ke/search-counties/#"
source = requests.get(hudumaUrl).text
soup = BeautifulSoup(source,'lxml')
table = soup.find("div", {"class":"wpb_wrapper"})
#table1 = table.find('table')
table1 = soup.find("table", {"class":"tablepress","id":"tablepress-3"})
#get the columns
table_head = table1.find("thead")
table_head_row = table_head.find("tr")
heading_list = []
for column in table_head_row.find_all("th"):
column_name = column.text
heading_list.append(column_name)
print(heading_list)
#get tbody
# =============================================================================
tb_tbody = table1.find("tbody")
tb_tbody_row = tb_tbody.find_all("tr")
#
table_data = []
for row in tb_tbody_row:
row_data = []
for data in row.find_all("td"):
row_data.append(data.text)
#print(data.text,end=" ")
table_data.append(row_data)
#print(table_data)
# =============================================================================
# Create a workbook and add a worksheet.
workbook = xlsxwriter.Workbook('counties.xlsx')
worksheet = workbook.add_worksheet()
# Adjust the column width.
worksheet.set_column(1, 1, 15)
# Add a bold format to use to highlight cells.
bold = workbook.add_format({'bold': 1})
# Write some data headers.
worksheet.write('A1', 'COUNTY_CODE', bold)
worksheet.write('B1', 'COUNTY', bold)
worksheet.write('C1', 'SUB_COUNTY', bold)
worksheet.write('D1', 'DIVISION', bold)
worksheet.write('E1', 'LOCATIONS', bold)
worksheet.write('F1', 'SUB_LOCATIONS', bold)
# Start from the first cell below the headers.
row = 1
col = 0
for COUNTY_CODE, COUNTY, SUB_COUNTY,DIVISION,LOCATIONS, SUB_LOCATIONS in (table_data):
worksheet.write_string (row, col, COUNTY_CODE )
worksheet.write_string (row, col + 1, COUNTY)
worksheet.write_string (row, col + 2, SUB_COUNTY)
worksheet.write_string (row, col + 3, DIVISION)
worksheet.write_string (row, col + 4, LOCATIONS)
worksheet.write_string (row, col + 5, SUB_LOCATIONS)
row += 1
workbook.close()