免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2429 | 回复: 5
打印 上一主题 下一主题

请教关于python登录网页的问题~ [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-08-06 14:31 |只看该作者 |倒序浏览
利用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
代码如下:
  1. # !/usr/bin/python
  2. import urllib2
  3. import urllib
  4. import cookielib
  5. import re
  6. import pdb
  7. #第一次request
  8. test_url = 'https://www.testurl.net/Select'
  9. cj = cookielib.CookieJar()
  10. opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
  11. urllib2.install_opener(opener)
  12. resp = urllib2.urlopen(test_url)
  13. #第二次request,cookiejar自动处理cookie
  14. auth_url = 'https://www.testurl.net/Account/Logon'
  15. data = {
  16.          "Email":"xxxxx",
  17.          "Password":"xxxxx"
  18.        }
  19. post_data = urllib.urlencode(data)
  20. headers = {
  21.            'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  22.            'accept-Encoding':'gzip',
  23.            'Accept-Language':'en-US,en;q=0.8',
  24.            'Cache-Control':'max-age=0',
  25.            'Connection':'keep-alive',
  26.            'Host': 'www.testurl.net',
  27.            'User-Agent':'Mozilla/6.0 (Windows NT 6.1; WOW64)'
  28.           }
  29. #post认证信息到认证页面auth_url
  30. req = urllib2.Request(auth_url, post_data, headers)
  31. opener.open(req)
  32. #打开要访问的页面test_url
  33. page = urllib2.urlopen(test_url)
  34. print page.read()
复制代码

论坛徽章:
0
2 [报告]
发表于 2014-08-06 16:55 |只看该作者
本帖最后由 halfcrazy 于 2014-08-06 16:56 编辑
  1. # !/usr/bin/python
  2. import urllib2
  3. import urllib
  4. import cookielib
  5. import re
  6. import pdb
  7. #第一次request
  8. test_url = 'https://www.testurl.net/Select'
  9. cj = cookielib.CookieJar()
  10. opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
  11. urllib2.install_opener(opener)
  12. resp = urllib2.urlopen(test_url)
  13. #第二次request,cookiejar自动处理cookie
  14. auth_url = 'https://www.testurl.net/Account/Logon'
  15. data = {
  16.          "Email":"xxxxx",
  17.          "Password":"xxxxx"
  18.        }
  19. post_data = urllib.urlencode(data)
  20. headers = {
  21.            'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  22.            'accept-Encoding':'gzip',
  23.            'Accept-Language':'en-US,en;q=0.8',
  24.            'Cache-Control':'max-age=0',
  25.            'Connection':'keep-alive',
  26.            'Host': 'www.testurl.net',
  27.            'User-Agent':'Mozilla/6.0 (Windows NT 6.1; WOW64)'
  28.           }
  29. #post认证信息到认证页面auth_url
  30. req = urllib2.Request(auth_url, post_data, headers)
  31. opener.open(req)
  32. #打开要访问的页面test_url
  33. #note here, remember to open the page with the opener with cookie
  34. page = opener.open(test_url)
  35. print page.read()
复制代码

论坛徽章:
0
3 [报告]
发表于 2014-08-07 10:16 |只看该作者
谢谢解答,测试过你的方法,结果还是session过期。{:3_199:} 请问,还有别的方法吗?{:2_167:} 回复 2# halfcrazy

论坛徽章:
9
2015亚冠之阿尔纳斯尔
日期:2015-09-10 16:21:162015亚冠之塔什干火车头
日期:2015-07-01 16:23:022015年亚洲杯之巴勒斯坦
日期:2015-04-20 17:19:46子鼠
日期:2014-11-13 09:51:26未羊
日期:2014-08-28 18:13:36技术图书徽章
日期:2014-02-21 09:30:15酉鸡
日期:2014-01-14 11:12:49天蝎座
日期:2013-12-09 17:56:53平安夜徽章
日期:2015-12-26 00:06:30
4 [报告]
发表于 2014-08-08 14:34 |只看该作者
回复 3# herlogh
  1. # !/usr/bin/python
  2. import urllib2
  3. import urllib
  4. import cookielib
  5. import re
  6. import pdb
  7. #第一次request
  8. test_url = 'https://www.testurl.net/Select'
  9. cj = cookielib.CookieJar()
  10. opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
  11. urllib2.install_opener(opener)
  12. resp = urllib2.urlopen(test_url)
  13. #第二次request,cookiejar自动处理cookie
  14. auth_url = 'https://www.testurl.net/Account/Logon'
  15. data = {
  16.          "Email":"xxxxx",
  17.          "Password":"xxxxx"
  18.        }
  19. post_data = urllib.urlencode(data)
  20. headers = {
  21.            'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  22.            'accept-Encoding':'gzip',
  23.            'Accept-Language':'en-US,en;q=0.8',
  24.            'Cache-Control':'max-age=0',
  25.            'Connection':'keep-alive',
  26.            'Host': 'www.testurl.net',
  27.            'User-Agent':'Mozilla/6.0 (Windows NT 6.1; WOW64)'
  28.           }
  29. #post认证信息到认证页面auth_url
  30. req = urllib2.Request(auth_url, post_data, headers)
  31. #打开要访问的页面test_url
  32. page = urllib2.urlopen(req)
  33. print page.read()
复制代码

论坛徽章:
0
5 [报告]
发表于 2014-08-08 16:15 |只看该作者
谢谢你的解答。测试过你的方法,直接request auth_url的话,返回结果是一堆乱码,估计是因为默认登录成功会跳转到test_url,而auth_url并不提供登录成功的页面。{:2_177:} 回复 4# HH106


   

论坛徽章:
0
6 [报告]
发表于 2014-08-10 15:04 |只看该作者
试试所有请求都用一开始绑定cookiejar的那个opener打开
回复 3# herlogh


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP