Last active
August 29, 2015 14:13
-
-
Save thedeetch/8c6477f7630f944fb675 to your computer and use it in GitHub Desktop.
Scrape Zoom Cable Modem Status Page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib, urllib2, cookielib | |
from bs4 import BeautifulSoup | |
import datetime | |
from time import sleep | |
import csv | |
def main(): | |
login_url = "http://192.168.100.1/goform/login" | |
status_url = "http://192.168.100.1/RgConnect.asp" | |
login_data = [ | |
('loginUsername', 'admin'), | |
('loginPassword', 'password') | |
] | |
headers = { | |
'Host': 'http://192.168.100.1/', | |
'Referer': 'http://192.168.100.1/', | |
'Origin': 'http://192.168.100.1/' | |
} | |
# Setup the cookie jar | |
cj = cookielib.CookieJar() | |
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) | |
opener.addheaders = headers.items() | |
# Login and get the cookies | |
resp = opener.open(login_url, urllib.urlencode(login_data)) | |
resp.close() | |
# Get the modem status | |
resp = opener.open(status_url) | |
status_html = resp.read() | |
resp.close() | |
format(status_html) | |
#format | |
#acq down channel status,comment,state status,comment,boot status,comment,config file status,comment, | |
def format(s): | |
# print status_html | |
r=[] | |
soup = BeautifulSoup(s) | |
r.append(datetime.datetime.now().isoformat()) | |
# first table | |
trs = soup.find_all("table")[1].find_all("tr") | |
r.append(trs[2].find_all('td')[1].text) | |
r.append(trs[2].find_all('td')[2].text) | |
r.append(trs[3].find_all('td')[1].text) | |
r.append(trs[3].find_all('td')[2].text) | |
r.append(trs[4].find_all('td')[1].text) | |
r.append(trs[4].find_all('td')[2].text) | |
r.append(trs[5].find_all('td')[1].text) | |
r.append(trs[5].find_all('td')[2].text) | |
r.append(trs[6].find_all('td')[1].text) | |
r.append(trs[6].find_all('td')[2].text) | |
# second table | |
snr=0.0 | |
p=0.0 | |
trs=soup.find_all("table")[2].find_all("tr") | |
for row in range(2,9): | |
for col in range(8): | |
t = trs[row].find_all('td')[col].text.replace(" dBmV", "").strip() | |
r.append(t) | |
if col == 5: | |
p += float(t) | |
if col == 6: | |
snr += float(t) | |
# average snr and power | |
r.append(p/8) | |
r.append(snr/8) | |
# third table | |
r.append(soup.find_all("table")[3].find_all("tr")[1].find_all('td')[0].text) | |
r.append(soup.find_all("table")[3].find_all("tr")[1].find_all('td')[1].text) | |
# fourth table | |
p=0.0 | |
trs=soup.find_all("table")[4].find_all("tr") | |
for row in range(2,5): | |
for col in range(7): | |
t = trs[row].find_all('td')[col].text.replace(" dBmV", "").strip() | |
r.append(t) | |
if col == 6: | |
p += float(t) | |
r.append(p/2) | |
print r | |
with open("output.csv",'ab') as f: | |
wr = csv.writer(f, dialect='excel') | |
wr.writerow(r) | |
if __name__ == "__main__": | |
while True: | |
main() | |
sleep(30) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment