濮阳杆衣贸易有限公司

主頁 > 知識庫 > python urllib.request模塊的使用詳解

python urllib.request模塊的使用詳解

熱門標簽:女王谷地圖標注 長春人工智能電銷機器人官網(wǎng) 地圖標注推廣單頁 ai地圖標注 n400電話申請多少錢 西藏快速地圖標注地點 廈門crm外呼系統(tǒng)如何 如何在地圖標注文字 百應(yīng)ai電銷機器人鄭州

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)

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

import urllib.request

url ='http://127.0.0.1:8848/chenjy/test.html'

req = urllib.request.urlopen(url)

print(req.read())

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

import urllib.request

url = 'http://127.0.0.1:8848/chenjy/test.html'

req = urllib.request.urlopen(url)

print(req.read().decode())

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

  • 創(chuàng)建一個測試頁
import urllib.request
url = 'http://127.0.0.1:8848/chenjy/test.html' 

req = urllib.request.urlopen(url)

print(req.geturl())

  • 前端重定向

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

  • 后端重定向

我們啟動一個項目并添加一個攔截器當訪問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.request
url = 'http://localhost:8088/ls/index.html'

req = urllib.request.urlopen(url)

print(req.geturl())

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

import urllib.request
url = 'http://127.0.0.1:8848/chenjy/test.html'

req = urllib.request.urlopen(url)

print(req.info())

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

import urllib.request
url = 'http://127.0.0.1:8848/chenjy/test.html'

req = urllib.request.urlopen(url)

print(req.getcode())

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模塊的使用詳解的詳細內(nèi)容,更多關(guān)于python urllib.request模塊的資料請關(guān)注腳本之家其它相關(guān)文章!

您可能感興趣的文章:
  • python爬蟲之利用Selenium+Requests爬取拉勾網(wǎng)
  • Python requests timeout的設(shè)置
  • python+requests+pytest接口自動化的實現(xiàn)示例
  • python3 解決requests出錯重試的問題
  • Python requests庫參數(shù)提交的注意事項總結(jié)
  • python requests完成接口文件上傳的案例
  • python爬取豆瓣電影排行榜(requests)的示例代碼
  • requests在python中發(fā)送請求的實例講解
  • python 實現(xiàn)Requests發(fā)送帶cookies的請求
  • python軟件測試Jmeter性能測試JDBC Request(結(jié)合數(shù)據(jù)庫)的使用詳解
  • python requests庫的使用
  • python實現(xiàn)文件+參數(shù)發(fā)送request的實例代碼
  • Python爬蟲基礎(chǔ)之requestes模塊

標簽:綿陽 拉薩 內(nèi)江 興安盟 亳州 黔東 廊坊 渭南

巨人網(wǎng)絡(luò)通訊聲明:本文標題《python urllib.request模塊的使用詳解》,本文關(guān)鍵詞  python,urllib.request,模塊,的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《python urllib.request模塊的使用詳解》相關(guān)的同類信息!
  • 本頁收集關(guān)于python urllib.request模塊的使用詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    辽宁省| 库尔勒市| 巴塘县| 乃东县| 彭水| 新沂市| 集贤县| 朝阳县| 东山县| 青铜峡市| 宜州市| 江源县| 资兴市| 宁化县| 景洪市| 漳浦县| 西华县| 铁岭市| 西乌珠穆沁旗| 凉山| 锦屏县| 阜新| 肥东县| 尚志市| 南和县| 乌恰县| 巨野县| 武邑县| 盖州市| 聂拉木县| 邵武市| 台州市| 绥德县| 舟山市| 尚义县| 惠来县| 台东市| 双桥区| 东乡| 霍城县| 龙泉市|