python2 urllib模块,python urllib2模块

python urllib2模块

urlopen()最常用的函数

urllib2.urlopen(url[, data[, timeout[, cafile[, capath[, cadefault[, context]]]]])

Open the URL url, which can be either a string or a Request object.

他有几个参数比较重要的

url 网址,http开头的,要写全。

data ,数据,可以使用urllib.urlencode()提供的数据。

urlopen常用用法

import urllib2

f = urllib2.urlopen('http://gqdw.xyz')

print f.read(100)

从http协议上来看,就是一个获取html页面的get请求,非常简单。

Request对象

class urllib2.Request(url[, data][, headers][, origin_req_host][, unverifiable])

This class is an abstraction of a URL request.

几个参数,url,data参数同上。

headers http请求头,是个字典类型的值。可以这里指定,也可以用Request.add_header(key,val)后面加上。

Request是一个抽象的url类,可以被urlopen调用。

Request post数据的例子

import urllib2

req = urllib2.Request(url='https://localhost/cgi-bin/test.cgi',

data='This data is passed to stdin of the CGI')

f = urllib2.urlopen(req)

print f.read()

http基本验证的例子

import urllib2

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')

基本用法是

1. 生成一个hander。

2. build_opener(handler)返回一个opener。

3. install_opener(opener)。

4. 最后就是熟悉的urlopen了。

也有另一种写法,就是不install_opener,直接用opener的open函数来访问url 。

例如下面这个

http cookie的例子

login_opt = urllib.urlencode({

"name": "test",

"password": "xxx",

"autologin": 1,

"enter": "Sign in"})

cj = cookielib.CookieJar()

opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))

login_url = "http://xxx/index.php"

data = opener.open(login_url,login_opt).read()

也可以写成这样:

login_opt = urllib.urlencode({

"name": "test",

"password": "xxx",

"autologin": 1,

"enter": "Sign in"})

cj = cookielib.CookieJar()

opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))

login_url = "http://xxx/index.php"

urllib2.install_opener(opener)

data = urllib2.urlopen(login_url,login_opt).read()

cookie模块很有用,有些网站必须登录了以后才能访问url,这时候就必须cookielib出马了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值