Python 3,urllib后submi

2024-06-26 00:27:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我想编写一个Python脚本来自动登录到我的宽带使用量表帐户。我以前从未做过投稿,现在遇到了一些麻烦。

import urllib.request, urllib.parse, urllib.error
import socket

try:
    details = urllib.parse.urlencode({ 'IDToken1': 'USERNAME', 'IDToken2': 'PASSWORD' })
    url = urllib.request.Request('https://login1.telecom.co.nz/distauth/UI/Login?realm=XtraUsers&goto=https%3A%2F%2Fwww.telecom.co.nz%3A443%2Fjetstreamum%2FxtraSum%3Flink%3Drdt', details)
    url.add_header("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.29 Safari/525.13")

    responseData = urllib.request.urlopen(url).read().decode('utf8', 'ignore')
    responseFail = False

except urllib.error.HTTPError as e:
    responseData = e.read().decode('utf8', 'ignore')
    responseFail = False

except urllib.error.URLError:
    responseFail = True

except socket.error:
    responseFail = True

except socket.timeout:
    responseFail = True

except UnicodeEncodeError:
    print("[x]  Encoding Error")
    responseFail = True

print(responseData)

我从HTML中导出IDToken1是用户名id,IDToken2是密码id

这是我的问题:

  • 当我输入正确的用户名和密码时,将加载登录页,但是:

  • 当我输入错误的用户名或密码时,我会看到一个页面,上面写着:

    This server has encountered an internal error which prevents it from fulfilling your request. The most likely cause is a misconfiguration. Please ask the administrator to look for messages in the server's error log.


Tags: importtrueurl密码parserequesterrorsocket
2条回答
details = urllib.parse.urlencode({'IDToken1': 'USERNAME', 'IDToken2': 'PASSWORD'})

添加以下行:

details = details.encode('UTF-8')

可能是故意的。如果你在浏览器中做,会发生什么?它使用正确的数据意味着你做得对。

相关问题 更多 >