- 论坛徽章:
- 0
|
本帖最后由 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()
复制代码 |
|