herlogh 发表于 2014-08-06 14:31

请教关于python登录网页的问题~

利用Python自动登录公司内部网址。
https://www.testurl.net/Account/Logon为认证页面。
https://www.testurl.net/Select为返回页面,当用户登录成功的时候,自动跳转到该页面。
运行结果提示session过期:You session has expired, Please sign in again。
请问这是因为在页面跳转的时候,cookie失效造成的吗,还是其它的原因?
有什么办法可以成功登录:https://www.testurl.net/Select
代码如下:# !/usr/bin/python
import urllib2
import urllib
import cookielib
import re
import pdb
#第一次request
test_url = 'https://www.testurl.net/Select'
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
resp = urllib2.urlopen(test_url)
#第二次request,cookiejar自动处理cookie
auth_url = 'https://www.testurl.net/Account/Logon'
data = {
         "Email":"xxxxx",
         "Password":"xxxxx"
       }
post_data = urllib.urlencode(data)
headers = {
         'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
         'accept-Encoding':'gzip',
         'Accept-Language':'en-US,en;q=0.8',
         'Cache-Control':'max-age=0',
         'Connection':'keep-alive',
         'Host': 'www.testurl.net',
         'User-Agent':'Mozilla/6.0 (Windows NT 6.1; WOW64)'
          }
#post认证信息到认证页面auth_url
req = urllib2.Request(auth_url, post_data, headers)
opener.open(req)
#打开要访问的页面test_url
page = urllib2.urlopen(test_url)
print page.read()

halfcrazy 发表于 2014-08-06 16:55

本帖最后由 halfcrazy 于 2014-08-06 16:56 编辑

# !/usr/bin/python
import urllib2
import urllib
import cookielib
import re
import pdb
#第一次request
test_url = 'https://www.testurl.net/Select'
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
resp = urllib2.urlopen(test_url)
#第二次request,cookiejar自动处理cookie
auth_url = 'https://www.testurl.net/Account/Logon'
data = {
         "Email":"xxxxx",
         "Password":"xxxxx"
       }
post_data = urllib.urlencode(data)
headers = {
         'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
         'accept-Encoding':'gzip',
         'Accept-Language':'en-US,en;q=0.8',
         'Cache-Control':'max-age=0',
         'Connection':'keep-alive',
         'Host': 'www.testurl.net',
         'User-Agent':'Mozilla/6.0 (Windows NT 6.1; WOW64)'
          }
#post认证信息到认证页面auth_url
req = urllib2.Request(auth_url, post_data, headers)
opener.open(req)
#打开要访问的页面test_url
#note here, remember to open the page with the opener with cookie
page = opener.open(test_url)
print page.read()

herlogh 发表于 2014-08-07 10:16

谢谢解答,测试过你的方法,结果还是session过期。{:3_199:} 请问,还有别的方法吗?{:2_167:} 回复 2# halfcrazy

HH106 发表于 2014-08-08 14:34

回复 3# herlogh # !/usr/bin/python
import urllib2
import urllib
import cookielib
import re
import pdb
#第一次request
test_url = 'https://www.testurl.net/Select'
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
resp = urllib2.urlopen(test_url)
#第二次request,cookiejar自动处理cookie
auth_url = 'https://www.testurl.net/Account/Logon'
data = {
         "Email":"xxxxx",
         "Password":"xxxxx"
       }
post_data = urllib.urlencode(data)
headers = {
         'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
         'accept-Encoding':'gzip',
         'Accept-Language':'en-US,en;q=0.8',
         'Cache-Control':'max-age=0',
         'Connection':'keep-alive',
         'Host': 'www.testurl.net',
         'User-Agent':'Mozilla/6.0 (Windows NT 6.1; WOW64)'
          }
#post认证信息到认证页面auth_url
req = urllib2.Request(auth_url, post_data, headers)
#打开要访问的页面test_url
page = urllib2.urlopen(req)
print page.read()

herlogh 发表于 2014-08-08 16:15

谢谢你的解答。测试过你的方法,直接request auth_url的话,返回结果是一堆乱码,估计是因为默认登录成功会跳转到test_url,而auth_url并不提供登录成功的页面。{:2_177:} 回复 4# HH106


   

halfcrazy 发表于 2014-08-10 15:04

试试所有请求都用一开始绑定cookiejar的那个opener打开
回复 3# herlogh


   
页: [1]
查看完整版本: 请教关于python登录网页的问题~