这里写自定义目录标题
- 阅读目录
- urllib.request.urlopen()
- 请求示例程序
- urlopen()提供的返回值方法
- urlopen()传递data参数
- urlopen()传递timeout参数
阅读目录
urllib是python内置的HTTP请求库,无需安装即可使用,它包含了4个模块:
request:它是最基本的http请求模块,用来模拟发送请求
error:异常处理模块,如果出现错误可以捕获这些异常
parse:一个工具模块,提供了许多URL处理方法,如:拆分、解析、合并等
robotparser:主要用来识别网站的robots.txt文件,然后判断哪些网站可以爬
urllib.request.urlopen()
urlopen
(
url
,
data
=
None
,
timeout
=
socket
.
_GLOBAL_DEFAULT_TIMEOUT
,
*
,
cafile
=
None
,
capath
=
None
,
cadefault
=
False
,
context
=
None
)
- url:请求地址
- data: Post请求提交的数据
- timeout: 超时时间
- cafile:这个是CA证书
- capath :这个是CA证书路径
- cadefault=False:默认没有CA证书
- context: 这个可以指定SSL安装验证设置,比如我们可以设置忽略证书验证等等。
请求示例程序
import
urllib
.
request
"""
urlopen(url, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,*
,cafile=None, capath=None, cadefault=False, context=None)
"""
resopnse
=
urllib
.
request
.
urlopen
(
"https://www.python.org"
)
urlopen()提供的返回值方法
- read(): 对HTTPResponse类型数据进行操作
print
(
type
(
resopnse
)
)
###
#读取数据,编码UTF-8
print
(
resopnse
.
read
(
)
.
decode
(
'UTF-8'
)
)
- status 状态吗
- getheaders() 头信息
- getheader(‘param’) 获取指定的头信息
print
(
resopnse
.
status
)
# 状态码
print
(
resopnse
.
getheaders
(
)
)
# 头信息
"""
[ ('Server', 'nginx'), ('Content-Type', 'text/html; charset=utf-8'),
('X-Frame-Options', 'DENY'), ('Via', '1.1 vegur'), ('Via', '1.1 varnish'),
('Content-Length', '48914'), ('Accept-Ranges', 'bytes'),
('Date', 'Mon, 16 Sep 2019 13:55:27 GMT'), ('Via', '1.1 varnish'),
('Age', '2874'), ('Connection', 'close'),
('X-Served-By', 'cache-iad2127-IAD, cache-lax8634-LAX'),
('X-Cache', 'HIT, HIT'), ('X-Cache-Hits', '1, 99'),
('X-Timer', 'S1568642127.467226,VS0,VE0'), ('Vary', 'Cookie'),
('Strict-Transport-Security', 'max-age=63072000; includeSubDomains')]
"""
print
(
resopnse
.
getheader
(
'Server'
)
)
# 获取服务器类型 nginx
urlopen()传递data参数
data
=
bytes
(
urllib
.
parse
.
urlencode
(
{
'word'
:
'hello'
}
)
,
encoding
=
'UTF-8'
)
resopnse
=
urllib
.
request
.
urlopen
(
'http://httpbin.org/post'
,
data
=
data
)
print
(
resopnse
.
read
(
)
)
规定data参数是byte类型。因此我们需要将参数转化为byte.调用byte()方法。第一个参数是String类型。第二个参数为编码。我们传递一个key为word,value为hello的参数。使用urllib.parse.urlencode将字典转化为string类型。最终返回的结果如下:
注意,传递data参数则必须是POST请求
{
"args"
:
{
}
,
"data"
:
""
,
"files"
:
{
}
,
"form"
:
{
"word"
:
"hello"
}
,
"headers"
:
{
"Accept-Encoding"
:
"identity"
,
"Content-Length"
:
"10"
,
"Content-Type"
:
"application/x-www-form-urlencoded"
,
"Host"
:
"httpbin.org"
,
"User-Agent"
:
"Python-urllib/3.5"
}
,
"json"
:
null
,
"origin"
:
"117.176.186.251, 117.176.186.251"
,
"url"
:
"https://httpbin.org/post"
}
可以看到我们的参数在form里面,因此作为表单去提交参数的。
urlopen()传递timeout参数
resopnse
=
urllib
.
request
.
urlopen
(
'http://httpbin.org/post'
,
data
=
data
,
timeout
=
1
)
File "/usr/lib/python3.5/http/client.py", line 1198, in getresponse
response.begin()
File "/usr/lib/python3.5/http/client.py", line 297, in begin
version, status, reason = self._read_status()
File "/usr/lib/python3.5/http/client.py", line 258, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "/usr/lib/python3.5/socket.py", line 576, in readinto
return self._sock.recv_into(b)
socket.timeout: timed out
设置1s超时时间,请求socket超时!
print
(
"********************************"
)
data
=
bytes
(
urllib
.
parse
.
urlencode
(
{
'word'
:
'hello'
}
)
,
encoding
=
'UTF-8'
)
try
:
resopnse
=
urllib
.
request
.
urlopen
(
'http://httpbin.org/post'
,
data
=
data
,
timeout
=
0.1
)
except
urllib
.
error
.
URLError
as
e
:
print
(
'TIME OUT'
)
print
(
resopnse
.
read
(
)
)