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 ...
随机推荐
- vue学习--组件之间的传值方式
1.概述 vue由多个组件构成页面,在不同的组件中有不同的联系,组件之间的传值是十分有必要的 2.父子组件之间传值 --props和$emit 父传子:通过props 方法:子组件:props:['m ...
- APICloud开发者进阶之路 | UIPickerView 模块示例demo
本文出自APICloud官方论坛 rongCloud2 3.2.8 版本更新后添加了发送小视频接口,发送文件接口. rongCloud2 概述 融云是国内首家专业的即时通讯云服务提供商,专注为互联 ...
- 通过VS2019使用Web部署发布.net core程序
服务器:Windows Server2012R2 服务器已安装好IIS 需要启用Web Management Service 与 Web部署代理服务 服务器默认是没有Web部署代理服务的 需要安装 ...
- 《C# 爬虫 破境之道》:概述
第一节:写作本书的目的 关于笔者 张晓亭(Mike Cheers),1982年出生,内蒙古辽阔的大草原是我的故乡. 没有高学历,没有侃侃而谈的高谈阔论,拥有的就是那一份对技术的执著,对自我价值的追求. ...
- Splash简单应用
jd->iphone import requests from lxml import etree # search_key = 'iphone' jd_url = "https:// ...
- 关于Hive中case when不准使用子查询的解决方法
在公司用Hive实现个规则的时候,遇到了要查询某个字段是否在另一张表中,大概情况就是 A表: id value1 value2 1 100 0 2 101 1 3 102 1 B表: value1 1 ...
- influxdb的命令们
InfluxDB是一个开源的时序数据库,使用GO语言开发,特别适合用于处理和分析资源监控数据这种时序相关数据.而InfluxDB自带的各种特殊函数如求标准差,随机取样数据,统计数据变化比等,使数据统计 ...
- xpath写法大全(适用于selenium、robotframework)
1.//input[contains(@id, 'txttags')] 定位出来是个ID,但是ID后面的“102”是个随机数,所以用定位ID的方法就不行了,用firepath生成的xpath也会包括这 ...
- python HelloWorld 的 4 种姿势,你知道几种
安装完 Python 之后该干啥,当然是要 say HelloWorld 了. python.exe 就是个普通程序 和其它所有命令一样,在命令行中敲下 python 并回车的时候,操作系统去 PAT ...
- Jutil 单元测试
eclipse 自身集成了jtuil,右击项目,点击bulidpath,addjar,如下所示添加jutil 新建一个Junit的测试类用来测试上面的测试方法,新增Junit的测试类方法如下: // ...