-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
72 lines (58 loc) · 2.58 KB
/
index.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
import imp
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from time import sleep
from linqit import List
from tabulate import tabulate
s = Service(ChromeDriverManager().install())
op = webdriver.ChromeOptions()
op.add_argument(r"start-maximized")
# op.add_experimental_option("detach", True);
driver = webdriver.Chrome(service=s, options=op)
driver.maximize_window()
# Mo trang Covid-19 cua Bo y te
driver.get(r"https://covid19.gov.vn/")
sleep(3)
# Chuyen sang frame co chua thong tin ve so ca Covid-19 cac tinh thanh
thongKeTinhThanhIframe = driver.find_element(
By.XPATH, r'//*[@id="chartCovidStatistics"]/iframe')
driver.switch_to.frame(thongKeTinhThanhIframe)
# Lay so lieu tung row trong bang du dieu, ke ca dong tieu de
def LaySoLieuDongTrongBang(dongDuLieuWebElement):
duLieuTinhThanhText = dongDuLieuWebElement.find_element(
By.CLASS_NAME, r'city').text
duLieuTongSoCaText = dongDuLieuWebElement.find_element(
By.CLASS_NAME, r'total').text
duLieu24GioQuaText = dongDuLieuWebElement.find_element(
By.CLASS_NAME, r'daynow').text
duLieuTuVongText = dongDuLieuWebElement.find_element(
By.CLASS_NAME, r'die').text
rowData = [duLieuTinhThanhText, duLieuTongSoCaText,
duLieu24GioQuaText, duLieuTuVongText]
return rowData
# Chuyen so lieu tu dang string sang dang so
def ChuyenDuLieuStringDongSangDuLieuInt(rowData):
tongSoCaInt = int(str(rowData[1]).replace(".", ""))
s24GioQuaString = str(rowData[2]).replace(".", "").replace("+", "").replace("-", "");
i24GioQuaInt = int(s24GioQuaString) if s24GioQuaString else 0;
tuVongInt = int(str(rowData[3]).replace(".", ""))
return [rowData[0], tongSoCaInt, i24GioQuaInt, tuVongInt]
# Lay tieu de cac truong thong tin
dongTieuDeThongKe = driver.find_element(
By.XPATH, r'//*[@id="location"]/div/div[1]')
header = LaySoLieuDongTrongBang(dongTieuDeThongKe)
# print(header);
# Lay so lieu thong ke cac tinh thanh
bangChuaDuLieuThongKe = driver.find_element(
By.XPATH, r'//*[@id="location"]/div/div[2]')
cacDongChuaDuLieuThongKe = List(
bangChuaDuLieuThongKe.find_elements(By.CLASS_NAME, r'row'))
duLieuThongKeCacTinhThanh = cacDongChuaDuLieuThongKe.select(
lambda x: LaySoLieuDongTrongBang(x)).select(lambda y: ChuyenDuLieuStringDongSangDuLieuInt(y))
# print(duLieuThongKeCacTinhThanh);
# Hien thi du lieu ra console
print(tabulate(duLieuThongKeCacTinhThanh, headers=header))
driver.close()