我正在尝试使用Ghost.py获取受密码保护的站点的HTML内容。
我必须访问的web服务器有以下HTML代码(我只将其削减到重要部分):
网址:http://192.168.1.60/PAGE.htm
<html>
<head>
<script language="JavaScript">
function DoHash()
{
var psw = document.getElementById('psw_id');
var hpsw = document.getElementById('hpsw_id');
var nonce = hpsw.value;
hpsw.value = MD5(nonce.concat(psw.value));
psw.value = '';
return true;
}
</script>
</head>
<body>
<form action="PAGE.HTM" name="" method="post" onsubmit="DoHash();">
Access code <input id="psw_id" type="password" maxlength="15" size="20" name="q" value="">
<br>
<input type="submit" value="" name="q" class="w_bok">
<br>
<input id="hpsw_id" type="hidden" name="pA" value="180864D635AD2347">
</form>
</body>
</html>每次加载页面时,"#hpsw_id“的值都会发生变化。
在普通浏览器上,一旦键入正确的密码并按enter或单击"submit“按钮,您就会登陆同一页面,但现在显示的是实际内容。
网址:http://192.168.1.60/PAGE.htm
<html>
<head>
<!–– javascript is gone ––>
</head>
<body>
Welcome to PAGE.htm content
</body>
</html>首先,我尝试了机械化,但失败了,因为我需要javascript。所以现在我想用Ghost.py来解决这个问题
到目前为止我的代码是:
import ghost
g = ghost.Ghost()
with g.start(wait_timeout=20) as session:
page, extra_resources = session.open("http://192.168.1.60/PAGE.htm")
if page.http_status == 200:
print("Good!")
session.evaluate("document.getElementById('psw_id').value='MySecretPassword';")
session.evaluate("document.getElementsByClassName('w_bok')[0].click();", expect_loading=True)
print session.content在我得到的控制台中,这段代码没有正确地加载内容:
追溯(最近一次调用):文件"",第8行,文件"/usr/local/lib/python2.7/dist-packages/ghost/ghost.py",第181行,包装器timeout=kwargs.pop(“超时”,无)文件"/usr/local/lib/python2.7/dist-packages/ghost/ghost.py",第1196行,wait_for_page_loaded中“无法加载请求的页面”,文件"/usr/local/lib/python2.7/dist-packages/ghost/ghost.py",第1174行,在wait_for page (Timeout_message)ghost.ghost.TimeoutError中:无法加载请求的页面
两个问题..。
1)如何成功登录到受密码保护的站点,获取PAGE.htm的真实内容?
2)这个方向是最好的选择吗?还是我完全错过了能让事情更有效率的东西?
我在用Ubuntu伙伴。
发布于 2018-07-30 14:36:10
--这是而不是--我一直在寻找的答案,只是一个解决办法--让它发挥作用(万一将来其他人也有类似的问题)。
为了跳过javascript部分(它阻止我使用python的请求),我决定在python上执行预期的散列操作(而不是在web上),并按照正常的web表单的方式发送散列。
因此Javascript基本上将隐藏的hpsw_id值和密码连接起来,并从中生成一个md5。
蟒蛇现在看起来如下所示:
import requests
from hashlib import md5
from re import search
url = "http://192.168.1.60/PAGE.htm"
with requests.Session() as s:
# Get hpsw_id number from website
r = s.get(url)
hpsw_id = search('name="pA" value="([A-Z0-9]*)"', r.text)
hpsw_id = hpsw_id.group(1)
# Make hash of ID and password
m = md5()
m.update(hpsw_id + 'MySecretPassword')
pA = m.hexdigest()
# Post to website to login
r = s.post(url, data=[('q', ''), ('q', ''), ('pA', pA)])
print r.content注意: q,Q和pA是表单(q=&q=&pA=f08b97e5e3f472fdde4280a9aa408aaa)在我正常使用互联网浏览器登录时发送的元素。
但是,如果有人知道我最初问题的答案,我将非常感谢你将它张贴在这里。
https://stackoverflow.com/questions/51552363
复制相似问题