urllib 模块 - module urllib
urllib 模块 - urllib module
获取 web 页面,
html = urllib.request.urlopen("http://www.zzyzz.top/")
html2 = urllib.request.Request("http://www.zzyzz.top/")
print("html",html)
print("html2",html2) output,
html <http.client.HTTPResponse object at 0x0395DFF0>
html2 <urllib.request.Request object at 0x03613930> Methods of HTTPResponse object,
geturl() — return the URL of the resource retrieved,
commonly used to determine if a redirect was followed
得到最终显示给用户的页面的 url (并不一定是所提供参数的 url, 因为有可能有
redirect 情况) info() — return the meta-information of the page, such as headers, in the
form of an email.message_from_string() instance (see Quick Reference
to HTTP Headers) getcode() – return the HTTP status code of the response. Methods of Request object,
Request.full_url
The original URL passed to the constructor.
Request.full_url is a property with setter, getter and a deleter.
Getting full_url returns the original request URL with the fragment,
if it was present.
即 'URL' 参数(区别于 HTTPResponse object 的 geturl() 方法) Request.type
The URI scheme.
'http' , 'https' 等 字符串 Request.host
The URI authority, typically a host, but may also contain a port
separated by a colon.
即 host IP Addr. (可能会同时得到 port 端口号) Request.origin_req_host
The original host for the request, without port.
即 host IP Addr, 不含 port 信息. Request.selector
The URI path. If the Request uses a proxy, then selector will be the
full URL that is passed to the proxy.
即 访问 server 的 path(相对于server 的 root 来说),
例如 '/' 表示 server root 跟目录. Request.data
The entity body for the request, or None if not specified.
例如 POST 的 form 信息. urllib.request.Request("http://www.zzyzz.top/",data)
# data = {"Hi":"Hello"} Request.unverifiable
boolean, indicates whether the request is unverifiable as defined by RFC 2965. Request.method
The HTTP request method to use. By default its value is None, which means
that get_method()will do its normal computation of the method to be used.
Its value can be set (thus overriding the default computation in get_method())
either by providing a default value by setting it at the class level in a
Request subclass, or by passing a value in to the Request constructor
via the method argument. Request.get_method()
Return a string indicating the HTTP request method. If Request.method
is not None,return its value, otherwise return 'GET' if Request.data
is None, or 'POST' if it’s not.This is only meaningful for HTTP requests.
'POST' 或者 'GET' Request.add_header(key, val)
Add another header to the request. Headers are currently ignored by
all handlers except HTTP handlers,where they are added to the list
of headers sent to the server. Note that there cannot be more than
one header with the same name, and later calls will overwrite previous
calls in case the key collides.Currently, this is no loss of HTTP
functionality, since all headers which have meaning when used more
than once have a (header-specific) way of gaining the same
functionality using only one header. Request.add_unredirected_header(key, header)
Add a header that will not be added to a redirected request. Request.has_header(header)
Return whether the instance has the named header (checks both
regular and unredirected). Request.remove_header(header)
Remove named header from the request instance (both from regular
and unredirected headers). Request.get_full_url()
Return the URL given in the constructor.
得到的其实是 Request.full_url Request.set_proxy(host, type)
Prepare the request by connecting to a proxy server. The host and
type will replace those of the instance, and the instance’s selector
will be the original URL given in the constructor. Request.get_header(header_name, default=None)
Return the value of the given header. If the header is not present,
return the default value. Request.header_items()
Return a list of tuples (header_name, header_value) of the Request headers. 例子, 获取 html codes,
urlobj = urllib.request.Request("http://www.zzyzz.top/")
with urllib.request.urlopen(urlobj) as FH: # 文件类对象
print(FH.read().decode('utf8')) Authentication,
当访问一个需要进行认证的 URL, 会得到一个 HTTP 401 错误,表示所访问的 URL 需要 Authentication.
Authentication 通常由种形式,
1, 浏览器 explorer 显示一个弹出框, 要求用户提供 用户名 密码进行认证, 它是基于 cookies 的.
2, form 表单形式的认证, 在 web 界面要求用户提供 用户名 密码, 然后通过 POST 方法将认证信息
发送给 server 端进行认证. 基于 cookies 的 Authentication 认证 - Basic HTTP Authentication
import urllib.request
# Create an OpenerDirector with support for Basic HTTP Authentication...
auth_handler = urllib.request.HTTPBasicAuthHandler()
auth_handler.add_password(realm= None,
uri="http://www.zzyzz.top/",
user='userid',
passwd='password')
opener = urllib.request.build_opener(auth_handler)
# ...and install it globally so it can be used with urlopen.
urllib.request.install_opener(opener)
html = urllib.request.urlopen("http://www.zzyzz.top/")
print(html.read().decode('utf8')) 基于 form 表单的 Authentication 认证,
再 server 端是通常这样处理, 对用户 submit(POST) 的 form 表单的数据信息做验证,
若验证通过 redirect 到授权页面, 否者 redirect 到 login 界面要求用户重新 POST
认证信息.
所以对于这一类的认证, 正常按照 POST form 的方法对待就可以了.
urlobj = urllib.request.Request("http://www.zzyzz.top/",{"id":"userid","pw":"password"})
with urllib.request.urlopen(urlobj) as FH: # 文件类对象
print(FH.read().decode('utf8')) 异常处理 - error handling
urllib 异常主要分为两类, 链接错误 跟 数据错误 链接类错误(错误的 URL 地址, URL 使用了一个不支持的协议,主机名不存在 等),
404 Page Not Found
链接过程中的异常是 urllib.request.URLError 的实例, 或其子类的实例.
比如, urllib.request.HTTPError, 其是一种文件类对象. 例子,
import sys, urllib.request
urlobj = urllib.request.Request("http://10.240.26.249/HELLO")
try:
with urllib.request.urlopen(urlobj) as FH:
print(FH.read().decode('utf8'))
except urllib.request.HTTPError as e:
print("HTTPError has been detected : ", e )
print("Error document :\n")
print(e.read().decode('utf8'))
sys.exit(1) except urllib.request.URLError as e:
print("URLError has been detected : ", e)
sys.exit(2) 数据类异常,
比如, 通信上的错误会使 socket 对象在调用 read() 方法时候发生 socket.error 异常.
或在数据传送过程中通信终断了 等等. Reference,
https://docs.python.org/3/library/urllib.request.html#module-urllib.request
urllib 模块 - module urllib的更多相关文章
- 全局变量 urllib模块 json模块
1.vars() 查看一个.py文件中的全局变量 print(vars()) #重点 __name__': '__main__ '__file__': 'C:/Users/lenovo/Pychar ...
- python3 urllib模块使用
urllib模块使用 urllib.request urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=N ...
- python学习笔记:网络请求——urllib模块
python操作网络,也就是打开一个网站,或者请求一个http接口,可以使用urllib模块.urllib模块是一个标准模块,直接import urllib即可,在python3里面只有urllib模 ...
- 【py网页】urllib模块,urlopen
Python urllib 库提供了一个从指定的 URL 地址获取网页数据,然后对其进行分析处理,获取想要的数据. 下面是在 Python Shell 里的 urllib 的使用情况: 01 Pyth ...
- Python内置的urllib模块不支持https协议的解决办法
Django站点使用django_cas接入SSO(单点登录系统),配置完成后登录,抛出“urlopen error unknown url type: https”异常.寻根朔源发现是python内 ...
- Python核心模块——urllib模块
现在Python基本入门了,现在开始要进军如何写爬虫了! 先把最基本的urllib模块弄懂吧. urllib模块中的方法 1.urllib.urlopen(url[,data[,proxies]]) ...
- Python3学习笔记(urllib模块的使用)转http://www.cnblogs.com/Lands-ljk/p/5447127.html
Python3学习笔记(urllib模块的使用) 1.基本方法 urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, ...
- python urllib模块的urlopen()的使用方法及实例
Python urllib 库提供了一个从指定的 URL 地址获取网页数据,然后对其进行分析处理,获取想要的数据. 一.urllib模块urlopen()函数: urlopen(url, data=N ...
- python爬虫-urllib模块
urllib 模块是一个高级的 web 交流库,其核心功能就是模仿web浏览器等客户端,去请求相应的资源,并返回一个类文件对象.urllib 支持各种 web 协议,例如:HTTP.FTP.Gophe ...
随机推荐
- file_get_contents函数获取不到数据的一种情况
问题: file_get_contents($url) 获取不到数据,尽管URL地址正确,函数使用正确.如下代码 $url = "https://www.baidu.com"; ...
- 【转】Hive Data Manipulation Language
Hive Data Manipulation Language Hive Data Manipulation Language Loading files into tables Syntax Syn ...
- docker安装mtproto及报错解决方案
安装docker:curl -sSL https://get.daocloud.io/docker | sh 给权限:usermod -aG docker [current_user] 启动:syst ...
- Scala与Mongodb实践1-----mongodbCRUD
目的:如何使用MongoDB之前提供有关Scala驱动程序及其异步API. 1.现有条件 IDEA中的:Scala+sbt+SDK mongodb-scala-driver的网址:http://mon ...
- docker启动报错 (iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport
今天修改完docker宿主机的防火墙文件 vim /etc/sysconfig/iptables 停止容器再启动时 报如下错误 (iptables failed: iptables --wait -t ...
- Java 成员变量与局部变量
成员变量与局部变量的区别 什么是局部变量.什么是成员变量: 局部变量:在方法内部的变量 形参:在方法签名中定义的变量. 方法局部变量:在方法内部定义的变量. 代码块局部变量:在代码块内定义的变量. 成 ...
- Office系列(1)---将Office文件(Word、PPT、Excel)转换为PDF文件
需求: 将Office文件作为文章并在网页上预览,主要为(Word.PPT.Excel)3种类型文件. 研究了一下,找到了两种解决方案 直接调用微软的在线预览功能实现(预览前提:预览资源必须可以直接通 ...
- Linux session(会话)
笔者在前文<Linux job control>中介绍了进程组(job)的概念以及常见的 job control 操作,本文接着介绍 session 的概念.本文中演示部分使用的环境为 u ...
- A*算法知识讲解
- 3次方的期望dp
osu 是一款群众喜闻乐见的休闲软件. 我们可以把osu的规则简化与改编成以下的样子: 一共有n次操作,每次操作只有成功与失败之分,成功对应1,失败对应0,n次操作对应为1个长度为n的 ...