色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

python urllib.request模塊的使用詳解

瀏覽:8日期:2022-06-24 15:27:59

python的urllib模塊提供了一系列操作url的功能,可以讓我們通過url打開任意資源。其中比較常用的就是request模塊,本篇主要介紹requset模塊。

urllib子模塊 urllib.request 打開或請(qǐng)求url urllib.error 捕獲處理請(qǐng)求時(shí)產(chǎn)生的異常 urllib.parse 解析url urllib.robotparser 用于解析robots.txt文件

robots.txt是一種存放于網(wǎng)站根目錄下文本文件,用來告訴網(wǎng)絡(luò)爬蟲服務(wù)器上的那些文件可以被查看。又被成為robots協(xié)議,是一種約定俗成的協(xié)議。

request模塊function request.urlopen()

urlopen方法用來打開資源url,常用帶參數(shù)形式urlopen(url,data=None),url:資源url,data:攜帶的數(shù)據(jù)。

方法的返回值始終為一個(gè)對(duì)象,并可以調(diào)用相應(yīng)的方法獲取返回的信息。其中對(duì)于http及https的url來說會(huì)返回一個(gè)http.client.HTTPResponse對(duì)象;

import urllib.request# 我們用本地的一個(gè)簡(jiǎn)單html文件來測(cè)試url = ’http://127.0.0.1:8848/chenjy/test.html’req = urllib.request.urlopen(url)print(req)

python urllib.request模塊的使用詳解

1. read() 返回服務(wù)器返回的原始數(shù)據(jù);

import urllib.requesturl =’http://127.0.0.1:8848/chenjy/test.html’req = urllib.request.urlopen(url)print(req.read())

python urllib.request模塊的使用詳解

我們可以再調(diào)用decode()方法來解碼。

import urllib.requesturl = ’http://127.0.0.1:8848/chenjy/test.html’req = urllib.request.urlopen(url)print(req.read().decode())

python urllib.request模塊的使用詳解

2.geturl() 返回獲取資源的url;

創(chuàng)建一個(gè)測(cè)試頁

import urllib.requesturl = ’http://127.0.0.1:8848/chenjy/test.html’ req = urllib.request.urlopen(url)print(req.geturl())

python urllib.request模塊的使用詳解

前端重定向

我們?cè)陧撁嬷刑砑觠s腳本重定向頁面window.location.href=’http://127.0.0.1:8848/chenjy/test2.html’;,會(huì)發(fā)現(xiàn)訪問的時(shí)候會(huì)重定向到test2,但是geturl還是獲取的重定向前的

python urllib.request模塊的使用詳解

后端重定向

我們啟動(dòng)一個(gè)項(xiàng)目并添加一個(gè)攔截器當(dāng)訪問index.html的時(shí)候重定向到/ls/html/list.html頁面,geturl獲取的是重定向后的頁面

@Override public void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {int index = target.lastIndexOf('index.html'); if (index != -1){HandlerKit.redirect('/ls/html/list.html',request,response,isHandled); } }

import urllib.requesturl = ’http://localhost:8088/ls/index.html’req = urllib.request.urlopen(url)print(req.geturl())

python urllib.request模塊的使用詳解

3.info() 返回頁面的元信息;

import urllib.requesturl = ’http://127.0.0.1:8848/chenjy/test.html’req = urllib.request.urlopen(url)print(req.info())

python urllib.request模塊的使用詳解

4.getcode() 返回頁面的狀態(tài)碼;

import urllib.requesturl = ’http://127.0.0.1:8848/chenjy/test.html’req = urllib.request.urlopen(url)print(req.getcode())

python urllib.request模塊的使用詳解

class request.Request

url請(qǐng)求類 Request(url, data=None, headers={}, origin_req_host=None, unverifiable=False, method=None)

url:請(qǐng)求url data:請(qǐng)求傳參;bytes字節(jié)流 headers:請(qǐng)求頭 origin_req_host:請(qǐng)求原始主機(jī);不帶端口 unverifiable:是否不可驗(yàn)證; method :請(qǐng)求方法;如GET、POST、PUT等

import urllib.request# 模擬iphone5請(qǐng)求百度手機(jī)版頁面url = ’https://www.baidu.com/’user_agent = ’Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1’headers = { ’User-Agent’: user_agent}# 抓取page信息req = urllib.request.Request(url, headers=headers,method=’GET’)page = urllib.request.urlopen(req).read().decode(’utf-8’)print(page)

python urllib.request模塊的使用詳解

以上就是python urllib.request模塊的使用詳解的詳細(xì)內(nèi)容,更多關(guān)于python urllib.request模塊的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 亚洲3级| 欧美一级手机免费观看片 | 免费国产在线观看 | 毛片免费在线观看网址 | 国内一级特黄女人精品片 | 国产成人精品亚洲77美色 | 国产欧美一区二区精品性色 | 久草资源在线播放 | 欧美aaaaa一级毛片在线 | 99精品国产成人一区二区 | 久久免费大片 | 欧美激情免费观看一区 | 亚洲精品资源在线 | 国产99视频精品免费观看9e | 久久国产精品-久久精品 | 性夜影院爽黄a爽免费看网站 | 杨幂丝袜国产福利视频 | 一区二区三区国产精品 | 精品国产成人高清在线 | 国产成人精品亚洲日本在线观看 | 午夜欧美成人久久久久久 | 欧美日韩在线观看一区二区 | 日本成本人视频 | 国产成人精品高清不卡在线 | 999国内精品永久免费视频 | 激情宗合 | 91色视| 日本一级爽毛片在线看 | 亚洲成a人片在线观 | 91无毒不卡 | 好妞操| 亚洲欧洲日韩在线 | 香蕉网站狼人久久五月亭亭 | 114毛片免费观看网站 | 亚洲午夜精品一级在线 | 精品欧美日韩一区二区三区 | 成人网18免费网 | 久久亚洲国产伦理 | 亚洲成人免费视频在线 | 成人高清毛片a | 亚洲国产成人久久综合碰 |