python urllib2模块

来源:互联网 发布:暴力摩托2004mac 编辑:程序博客网 时间:2024/05/23 12:25

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 urllib2f = 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 urllib2req = 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 urllib2auth_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
原创粉丝点击