Python urllib2 Note
http://docs.python.org/library/urllib2.html记一些我的理解
通常情况需要urllib2.build_opener()生成一个opener,并通过该opener来访问URL
proxy_handler = urllib2.ProxyHandler({'http': 'http://www.example.com:3128/'})
proxy_auth_handler = urllib2.ProxyBasicAuthHandler()
proxy_auth_handler.add_password('realm', 'host', 'username', 'password')
opener = urllib2.build_opener(proxy_handler, proxy_auth_handler)
# This time, rather than install the OpenerDirector, we use it directly:
opener.open('http://www.example.com/login.html')
另一种方式是直接用urllib2.urlopen(url[, data][, timeout])来直接访问URL
此时可以用urllib2.install_opener(opener)把一些需要全局应用的opener加入urllib2中
import urllib2
# Create an OpenerDirector with support for Basic HTTP Authentication...
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password(realm='PDQ Application',
uri='https://mahler:8092/site-updates.py',
user='klem',
passwd='kadidd!ehopper')
opener = urllib2.build_opener(auth_handler)
# ...and install it globally so it can be used with urlopen.
urllib2.install_opener(opener)
urllib2.urlopen('http://www.example.com/login.html')
附登录HDOJ看Rank的代码
#!/usr/bin/python
import urllib, urllib2, cookielib
cid = 351
cookiejar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
login_url = r'http://acm.hdu.edu.cn/userloginex.php?action=login&cid=%s¬ice=0' % cid
token = urllib.urlencode({'username': 'team183', 'userpass': '687017'})
opener.open(login_url, token)
for ck in cookiejar: print ck
rank_url = r'http://acm.hdu.edu.cn/contests/contest_ranklist.php?cid=%s&withid=0' % cid
res = opener.open(rank_url)
print res, res.geturl(), res.info(), res.read()
页:
[1]