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

您的位置:首頁技術文章
文章詳情頁

python urllib.request模塊的使用詳解

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

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

urllib子模塊 urllib.request 打開或請求url urllib.error 捕獲處理請求時產生的異常 urllib.parse 解析url urllib.robotparser 用于解析robots.txt文件

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

request模塊function request.urlopen()

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

方法的返回值始終為一個對象,并可以調用相應的方法獲取返回的信息。其中對于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() 返回服務器返回的原始數據;

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

python urllib.request模塊的使用詳解

我們可以再調用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;

創建一個測試頁

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’;,會發現訪問的時候會重定向到test2,但是geturl還是獲取的重定向前的

python urllib.request模塊的使用詳解

后端重定向

我們啟動一個項目并添加一個攔截器當訪問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() 返回頁面的狀態碼;

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字節流 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模塊的使用詳解的詳細內容,更多關于python urllib.request模塊的資料請關注好吧啦網其它相關文章!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 久久精品国产亚洲综合色 | 一级毛片私人影院老司机 | 日韩国产欧美精品综合二区 | 欧美一区二区三区在线 | 99久久一区| 婷婷在线成人免费观看搜索 | 国产一级精品毛片 | 日韩一级欧美一级毛片在线 | 亚洲毛片在线 | 亚洲wu码| 国产一级特黄全黄毛片 | 亚欧视频在线 | 在线视频一区二区三区三区不卡 | 九九九九九九 | 日本巨乳中文字幕 | 日本亚洲成高清一区二区三区 | 7777视频| 日韩三级观看 | 在线观看亚洲专区 | 亚洲男人在线 | 欧美一级看片a免费观看 | 日韩黄色一级毛片 | 91精品福利手机国产在线 | 在线观看二区三区午夜 | 国产黄色片在线观看 | 亚洲精品午夜 | 欧美野外性k8播放性迷宫 | 免费黄色欧美 | 亚洲国产中文字幕 | 成人交性视频免费看 | 成人欧美日韩高清不卡 | a毛片免费观看完整 | 久草5| 一区二区三区四区国产精品 | 国产精品久久久久免费a∨ 国产精品久久久久免费视频 | 免费一级毛片在线播放欧美 | 成人精品一区二区三区中文字幕 | 亚洲日韩中文字幕天堂不卡 | 午夜一级毛片不卡 | 免费视频毛片 | 亚洲看黄|