python項目之 抓取動態網頁 抓取路由器客戶

python項目之 抓取動態網頁 抓取路由器客戶

前身

前面有一片文章寫得是爬取路由器的客戶,使用模擬瀏覽器登錄的方式得到的。

python項目之 路由器抓取器
地址爲:http://blog.csdn.net/lyffly2011/article/details/50485398

改進

在學習完前端設計的知識後,意識到可以通過HTTP請求,直接得到動態的數據。

實現思路

  1. 打開瀏覽器調試功能,F12
  2. 分析瀏覽器數據流量的XHR,得到請求網址和數據
  3. 模擬請求,得到結果,進行解析
    其餘和之前類似。

注意點爲:cookie,http post中的payload,傳送字符串的換行。

具體代碼爲:

# coding : utf-8
####################################################
# coding by 劉雲飛
####################################################

import os
import time
import datetime
import requests
import base64

# 此處天填上自己的路由器密碼
MIMA = b'xxxxxxxx'
encode_MIMA = base64.b64encode(MIMA).decode("utf-8")
cookie = 'Authorization=Basic ' + encode_MIMA

headers1 = {
    'Accept': '*/*',
    'Accept-Encoding': 'gzip, deflate,sdch',
    'Accept-Language': 'zh-CN,zh;q=0.8',
    'Connection': 'keep-alive',
    'Content-Type': 'text/plain',
    'Cookie': cookie,
    'Host': '192.168.11.1',
    'Referer': 'http://192.168.11.1/',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36\
                  (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36',
}

headers = {
    'Accept': '*/*',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'zh-CN,zh;q=0.8',
    'Connection': 'keep-alive',
    'Content-Length': '98',
    'Content-Type': 'text/plain',
    'Cookie': cookie,
    'Host': '192.168.11.1',
    'Origin': 'http://192.168.11.1',
    'Referer': 'http://192.168.11.1/',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36\
                  (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36',
}

url_dhcpclient = 'http://192.168.11.1/main/dhcpClient.htm?_='

session = requests.Session()

time_ms = str(int(time.time() // 1)) + '012'
url_dhcpclient += time_ms
res1 = session.get(url_dhcpclient, headers=headers1)
print(res1.status_code)
params = '[LAN_HOST_ENTRY#0,0,0,0,0,0#0,0,0,0,0,0]0,4\r\nleaseTimeRemaining\r\nMACAddress\r\nhostName\r\nIPAddress\r\n'
url = 'http://192.168.11.1/cgi?5'
res = session.post(url=url, data=params, headers=headers)
# print(res.request.headers)
# print(res.url)
# print(res.status_code)
# print(res.headers)
print(res.text)

now = datetime.datetime.now()
str_time = now.strftime("%Y_%m_%d_%H_%M_%S")
text = res.text
filename = str_time + '.txt'
with open(filename, 'w+') as f:
    f.write(text)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章