這里寫(xiě)自定義目錄標(biāo)題
- 閱讀目錄
- urllib.request.urlopen()
- 請(qǐng)求示例程序
- urlopen()提供的返回值方法
- urlopen()傳遞data參數(shù)
- urlopen()傳遞timeout參數(shù)
閱讀目錄
urllib是python內(nèi)置的HTTP請(qǐng)求庫(kù),無(wú)需安裝即可使用,它包含了4個(gè)模塊:
request:它是最基本的http請(qǐng)求模塊,用來(lái)模擬發(fā)送請(qǐng)求
error:異常處理模塊,如果出現(xiàn)錯(cuò)誤可以捕獲這些異常
parse:一個(gè)工具模塊,提供了許多URL處理方法,如:拆分、解析、合并等
robotparser:主要用來(lái)識(shí)別網(wǎng)站的robots.txt文件,然后判斷哪些網(wǎng)站可以爬
urllib.request.urlopen()
urlopen
(
url
,
data
=
None
,
timeout
=
socket
.
_GLOBAL_DEFAULT_TIMEOUT
,
*
,
cafile
=
None
,
capath
=
None
,
cadefault
=
False
,
context
=
None
)
- url:請(qǐng)求地址
- data: Post請(qǐng)求提交的數(shù)據(jù)
- timeout: 超時(shí)時(shí)間
- cafile:這個(gè)是CA證書(shū)
- capath :這個(gè)是CA證書(shū)路徑
- cadefault=False:默認(rèn)沒(méi)有CA證書(shū)
- context: 這個(gè)可以指定SSL安裝驗(yàn)證設(shè)置,比如我們可以設(shè)置忽略證書(shū)驗(yàn)證等等。
請(qǐng)求示例程序
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(): 對(duì)HTTPResponse類(lèi)型數(shù)據(jù)進(jìn)行操作
print
(
type
(
resopnse
)
)
###
#讀取數(shù)據(jù),編碼UTF-8
print
(
resopnse
.
read
(
)
.
decode
(
'UTF-8'
)
)
- status 狀態(tài)嗎
- getheaders() 頭信息
- getheader(‘param’) 獲取指定的頭信息
print
(
resopnse
.
status
)
# 狀態(tài)碼
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'
)
)
# 獲取服務(wù)器類(lèi)型 nginx
urlopen()傳遞data參數(shù)
data
=
bytes
(
urllib
.
parse
.
urlencode
(
{
'word'
:
'hello'
}
)
,
encoding
=
'UTF-8'
)
resopnse
=
urllib
.
request
.
urlopen
(
'http://httpbin.org/post'
,
data
=
data
)
print
(
resopnse
.
read
(
)
)
規(guī)定data參數(shù)是byte類(lèi)型。因此我們需要將參數(shù)轉(zhuǎn)化為byte.調(diào)用byte()方法。第一個(gè)參數(shù)是String類(lèi)型。第二個(gè)參數(shù)為編碼。我們傳遞一個(gè)key為word,value為hello的參數(shù)。使用urllib.parse.urlencode將字典轉(zhuǎn)化為string類(lèi)型。最終返回的結(jié)果如下:
注意,傳遞data參數(shù)則必須是POST請(qǐng)求
{
"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"
}
可以看到我們的參數(shù)在form里面,因此作為表單去提交參數(shù)的。
urlopen()傳遞timeout參數(shù)
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
設(shè)置1s超時(shí)時(shí)間,請(qǐng)求socket超時(shí)!
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
(
)
)
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
