前言:我知道有很多类似问题的回答,例如堆栈溢出。但是,我还没有找到任何与 aspx 登录相关的信息,也没有找到像这样的确切案例。
问题:我需要确定登录https://cableone.net/login.aspx所需的信息,以便从那里抓取信息。
进展: 到目前为止,我已经在 login.aspx 的源代码中找到了输入字段,并在 python 中用 urllib、urllib2 和 cookielib 拼凑了一个脚本。我忽略了脚本中包含空白值的任何内容。
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"value="/wEPDwUIMzc1NzEwOTZkZFAEfkjXC+VNsqYoayGxa5/q4srT" />
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWBAK6lKDUCwLVx7ufCQL/+N3OBwLFgNGYD6KeUd6uNDBwc5zcR0u4hqrwv1fM" />
<input name="ctl00$plhMain$txtUserName" type="text" id="ctl00_plhMain_txtUserName" />
<input name="ctl00$plhMain$txtPassword" type="password" id="ctl00_plhMain_txtPassword" />
<input type="submit" name="ctl00$plhMain$btnLogin" value="Login" id="ctl00_plhMain_btnLogin" />
然后,我在下面将上述输入值与 python 和 urllib 一起使用。
import urllib, urllib2, cookielib
from cookielib import CookieJar
url = 'https://myaccount.cableone.net/Login.aspx'
cj = CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
cookies = cookielib.CookieJar()
#determine what I need to change with these values
formValues = {
"__VIEWSTATE":"/wEPDwUIMzc1NzEwOTZkZFAEfkjXC+VNsqYoayGxa5/q4srT",
"__EVENTVALIDATION":"/wEWBAK6lKDUCwLVx7ufCQL/+N3OBwLFgNGYD6KeUd6uNDBwc5zcR0u4hqrwv1fM",
"ctl00$plhMain$txtUserName":"myAccount",
"ctl00$plhMain$txtPassword":"myPassword"
}
data = urllib.urlencode(formValues)
response = opener.open("https://myaccount.cableone.net/Login.aspx",data)
thePage = response.read()
httpheaders = response.info()
print thePage