import requests #auto class
import webbrowser
import re
url='http://teaching.nkfust.edu.tw/Course/Login.aspx'
UserName='*****'
Password='******'
my_headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-TW,zh;q=0.8,en-US;q=0.6,en;q=0.4',
}
sss=requests.session()
r=sss.get(url,headers=my_headers)
reg=r'<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="(.*)" />'
pattern = re.compile(reg)
result = pattern.findall(r.content)
viewstate = result[0]
reg=r'<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="(.*)" />'
pattern = re.compile(reg)
result = pattern.findall(r.content)
eventvalidation=result[0]
my_data={'__EVENTTARGET':'',
'__EVENTARGUMENT':'',
'__VIEWSTATE':viewstate,
'__VIEWSTATEGENERATOR':'EC620E5A',
'__PREVIOUSPAGE':'cMlf7B-ve9HBkgfrfv-q8AJ50_VEsc0av7p79gkEJJkZqg6TfVcScwYFuXh9Bxb7aIieofwU1eB980nSeO55lrEIVXs1',
'__EVENTVALIDATION':eventvalidation,
'Login1$UserName':UserName,
'Login1$Password':Password,
'Login1$LoginButton.x':'0',
'Login1$LoginButton.y':'0',
}
r= sss.post(url, headers = my_headers, data = my_data)
print r.url, r.status_code, r.history
webbrowser.open(r.url, new=0, autoraise=True)
我想问为什么r.url = http://teaching.nkfust.edu.tw/Course/student/today.aspx
webbrowser.open(r.url,new = 0,autoraise = True)这是开放的 http://teaching.nkfust.edu.tw/Course/Login.aspx?ReturnUrl=%2FCourse%2Fstudent%2Ftoday.aspx
答案 0 :(得分:0)
如果我的问题得到了正确的解答,您的网址就会被更改为&#34;使用request.get()时。这是一种常见的重定向。所以你必须不允许它。
r=sss.get(url,headers=my_headers, allow_redirects=False)