spark web信息获取

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
# -*- coding: utf-8 -*-

import re
import requests


def _get_requests(url):
response = requests.get(url)
content = ""
if response.status_code == 200:
content = response.content.decode()
return content

def _get_spark_common(st):
s = re.findall(r'<ul class="unstyled".*</ul>', body, re.S)[0]
s = re.sub(r'\s{2,}|<a.*?>|</a>', ' ', s)
print_format = "{:15}{}".format
print("********************************")
info = re.findall(r'<strong>(.*?)</strong>\s*(.*?)\s*</li>', s)
for k, v in info:
print(print_format(k, v))
else:
print("\n")

def _get_spark_works(st):
s = re.findall(r'<div class="aggregated-workers collapsible-table".*?</div>', body, re.S)[0]
s = re.sub(r'<a.*>|</a>', "", s)
s = re.sub(r'\s+', " ", s)
head = re.findall(r'<th .*?>(.*?)</th>', s)
work_info = [re.findall(r'<td.*?>\s*(.*?)\s*</td>', x) for x in re.findall(r'<tr>.*?</tr>', s)]
work_info.insert(0, head)
print_format = "{:<46}{:25}{:10}{:15}{}".format
print("****************Workers (%s)****************" % (len(work_info) - 1))
for row in work_info:
print(print_format(*row))
else:
print("\n")


def _get_spark_running_applications(st):
s = re.findall(r'<div class="aggregated-activeApps.*?</div>', body, re.S)[0]
s = re.sub(r'\s+', " ", s)
s = re.sub(r'<a.*?>|</a>|<form.*?>.*</form>', "", s, re.S)
head = re.findall(r'<th .*?>(.*?)</th>', s)
run_work_info = [re.findall(r'<td.*?>\s*(.*?)\s*</td>', x) for x in re.findall(r'<tr>.*?</tr>', s)]
run_work_info.insert(0, head)
print_format = "{:<28}{:20}{:10}{:25}{:25}{:10}{:13}{}".format
print("****************Running Applications (%s)***************" % (len(run_work_info) - 1))
for row in run_work_info:
print(print_format(*row))
else:
print("\n")

def _get_spark_completed_applications(st):
s = re.findall(r'<div class="aggregated-completedApps.*?</div>', body, re.S)[0]
s = re.sub(r'\s+', " ", s)
s = re.sub(r'<a.*?>|</a>|<form.*?>.*</form>', "", s, re.S)
head = re.findall(r'<th .*?>(.*?)</th>', s)
run_work_info = [re.findall(r'<td.*?>\s*(.*?)\s*</td>', x) for x in re.findall(r'<tr>.*?</tr>', s)]
run_work_info.insert(0, head)
print_format = "{:<28}{:20}{:10}{:25}{:25}{:10}{:13}{}".format
print("****************Completed Applications (%s)****************" % (len(run_work_info) - 1))
for row in run_work_info:
print(print_format(*row))
else:
print("\n")


if __name__ == "__main__":
url = "http://192.168.100.10:8080/"

body = _get_requests(url)
spark_common_info = _get_spark_common(body)
_get_spark_works(body)
_get_spark_running_applications(body)
_get_spark_completed_applications(body)
-------------本文结束感谢您的阅读-------------