2013-08-29 44 views
2

我想從路由器獲取頁面192.168.1.1 /basic/home_dhcplist.htm ,但它在開始時要求輸入用戶名和密碼。如何從Python訪問受密碼保護的路由器頁面?

我通過urllib2的

import urllib2 
response = urllib2.urlopen('http://192.168.1.1/basic/home_dhcplist.htm') 
html = response.read() 
str="Prasads" 
value= html.find(str) 
print value 
if value!=-1 : 
    print "found" 
else: 
print "not found" 

response.close() 
+0

路由器的細節將不勝感激 –

回答

1

我見過的每個家庭路由器都使用基本身份驗證進行身份驗證。這只是您與請求一起發送的另一個標頭。每次請求頁面時,用戶名和密碼都會作爲標題發送到服務器,並在每個請求中對其進行驗證。

我建議requests圖書館超過urllib2

import requests 

r = requests.get('http://192.168.1.1/basic/home_dhcplist.htm', auth=('username', 'password')) 

if 'Prasads' in r.text(): 
    print "found" 
else: 
    print "not found" 
+0

我解決了認證的問題。 但是,我可以使用請求自動點擊一個按鈕嗎? 像一些網站我同意按鈕。 – skarfa

0

獲取Python中的頁面基本上你需要set the cookie它保持該會話,最有可能。

通過瀏覽器(Firefox)訪問該頁面時提示輸入登錄通行證。

按下Ctrl鍵移-K,並重新加載頁面,點擊任何最近GET的請求,你會得到展示GET請求詳細信息的窗口。請注意0​​並相應地設置cookie。

鍵值這將是最有用的是Authorization