亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

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

python urllib.request模塊的使用詳解

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

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

urllib子模塊 urllib.request 打開或請求url urllib.error 捕獲處理請求時產(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ù)。

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

import urllib.request# 我們用本地的一個簡單html文件來測試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)建一個測試頁

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

python urllib.request模塊的使用詳解

前端重定向

我們在頁面中添加js腳本重定向頁面window.location.href=’http://127.0.0.1:8848/chenjy/test2.html’;,會發(fā)現(xiàn)訪問的時候會重定向到test2,但是geturl還是獲取的重定向前的

python urllib.request模塊的使用詳解

后端重定向

我們啟動一個項目并添加一個攔截器當(dāng)訪問index.html的時候重定向到/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請求類 Request(url, data=None, headers={}, origin_req_host=None, unverifiable=False, method=None)

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

import urllib.request# 模擬iphone5請求百度手機版頁面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模塊的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 国产精品久久婷婷六月丁香 | 久青草国产高清在线视频 | 国产成人a一在线观看 | 国产一区二区不卡视频 | 日韩欧美毛片免费观看视频 | 亚洲精品一区二区三区美女 | 8888四色奇米在线观看不卡 | 国产精品一区伦免视频播放 | 日本高清xxxx免费视频 | 青草视频在线免费观看 | 午夜特片网| 亚洲国产精品日韩在线观看 | 国产日产亚洲欧美综合另类 | 欧美xxxx狂喷水喷水 | 国内精品久久久久鸭 | 午夜天堂在线视频 | 久久精品亚洲精品国产欧美 | 国产一级免费在线观看 | 国产成+人+综合+亚洲欧美丁香花 | 91精品久久久久久久久中文字幕 | 国产美女91呻吟求 | 簧片在线免费看 | 亚洲综合一区二区精品久久 | 妇女毛片| 一级黄色免费观看 | 黄色毛片免费 | 宅男69免费永久网站 | 亚洲精品午夜aaa级久久久久 | 亚洲精品99久久一区二区三区 | 国产免费观看视频 | 国产不卡视频在线观看 | 日韩一区二区三区在线播放 | 亚洲在线观看一区二区 | 色青青草原桃花久久综合 | 在线播放成人高清免费视频 | 青青青免费视频精品99 | 亚洲高清免费视频 | 国产精品嫩草影院在线 | 日韩欧美成人乱码一在线 | 亚洲天堂啪啪 | 日韩欧美一区二区久久黑人 |