python3的urllib的request模块

来源:互联网 发布:零基础学java第4版pdf 编辑:程序博客网 时间:2024/06/01 21:06
  1. urlopen
    定义:
    urlopen(url, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
    *, cafile=None, capath=None, cadefault=False, context=None)
    功能:像读文件一样读网页
#!/usr/bin/env python3# -*- coding:utf-8 -*-"打开网页"from urllib import requestbaidu = request.urlopen('http://www.baidu.com')baidu.readline()

2.urlretrieve
urlretrieve(url, filename=None, reporthook=None, data=None)
功能:下载网页到文件

#!/usr/bin/env python3# -*- coding:utf-8 -*-"打开网页"from urllib import requestbaidu = request.urlretrieve('http://www.baidu.com','/www/python/baidu.html')#如果urlretrieve不传文件名的话会自动生成,运行完程序可以删除request.urlcleanup()

3.urlcleanup
清除urlretrieve产生的临时文件,及opener对象
4.Request对象

class Request:    def __init__(self, url, data=None, headers={},                 origin_req_host=None, unverifiable=False,                 method=None)# url是网址,data数据,header头消息,origin_req_host相当于referrermethod请求的方法(如getpost

示例代码:

#!/usr/bin/env python3# -*- coding:utf-8 -*-from urllib.request import Requestrq = Request('http://www.baidu.com')这个类的主要干的事操作url,data,header@property 可以像访问属性一样访问方法@name.setter 可以向设置属性一样设置调用方法@name.deleter 删除属性
  1. request_host(request)
    得到request对象的主机
0 0
原创粉丝点击