Python urllib2 Note

本文介绍了如何使用Python的urllib2模块进行网页登录及抓取数据的操作。具体包括设置代理服务器、处理基本认证、利用Cookie保持登录状态等实用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://docs.python.org/library/urllib2.html

记一些我的理解

通常情况需要urllib2.build_opener([handler, ...])生成一个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&notice=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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值