自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

使用Python的urlliib.parse庫解析URL

開發(fā) 后端
Python 中的 urllib.parse 模塊提供了很多解析和組建 URL 的函數(shù)。urlparse() 函數(shù)可以將 URL 解析成 ParseResult 對象。

使用Python的urlliib.parse庫解析URL

Python 中的 urllib.parse 模塊提供了很多解析和組建 URL 的函數(shù)。 

解析url

urlparse() 函數(shù)可以將 URL 解析成 ParseResult 對象。對象中包含了六個元素,分別為:

  • 協(xié)議(scheme)
  • 域名(netloc)
  • 路徑(path)
  • 路徑參數(shù)(params)
  • 查詢參數(shù)(query)
  • 片段(fragment)
  1. from urllib.parse import urlparse
  2.  
  3. url='http://user:pwd@domain:80/path;params?query=queryarg#fragment'
  4.  
  5. parsed_result=urlparse(url)
  6.  
  7. print('parsed_result 包含了',len(parsed_result),'個元素')
  8. print(parsed_result)

結(jié)果為:

  1. parsed_result 包含了 6 個元素
  2. ParseResult(scheme='http', netloc='user:pwd@domain:80', path='/path', params='params', query='query=queryarg', fragment='fragment')

ParseResult 繼承于 namedtuple,因此可以同時通過索引和命名屬性來獲取 URL 中各部分的值。

為了方便起見, ParseResult 還提供了 username、 password、 hostname、 port 對 netloc 進(jìn)一步進(jìn)行拆分。

  1. print('scheme :', parsed_result.scheme)
  2. print('netloc :', parsed_result.netloc)
  3. print('path :', parsed_result.path)
  4. print('params :', parsed_result.params)
  5. print('query :', parsed_result.query)
  6. print('fragment:', parsed_result.fragment)
  7. print('username:', parsed_result.username)
  8. print('password:', parsed_result.password)
  9. print('hostname:', parsed_result.hostname)
  10. print('port :', parsed_result.port)

結(jié)果為:

  1. scheme : http
  2. netloc : user:pwd@domain:80
  3. path : /path
  4. params : params
  5. query : query=queryarg
  6. fragment: fragment
  7. username: user
  8. password: pwd
  9. hostname: domain
  10. port : 80

除了 urlparse() 之外,還有一個類似的 urlsplit() 函數(shù)也能對 URL 進(jìn)行拆分,所不同的是, urlsplit() 并不會把 路徑參數(shù)(params) 從 路徑(path) 中分離出來。

當(dāng) URL 中路徑部分包含多個參數(shù)時,使用 urlparse() 解析是有問題的:

  1. url='http://user:pwd@domain:80/path1;params1/path2;params2?query=queryarg#fragment'
  2.  
  3. parsed_result=urlparse(url)
  4.  
  5. print(parsed_result)
  6. print('parsed.path :', parsed_result.path)
  7. print('parsed.params :', parsed_result.params)

結(jié)果為:

  1. ParseResult(scheme='http', netloc='user:pwd@domain:80', path='/path1;params1/path2', params='params2', query='query=queryarg', fragment='fragment')
  2. parsed.path : /path1;params1/path2
  3. parsed.params : params2

這時可以使用 urlsplit() 來解析:

  1. from urllib.parse import urlsplit
  2. split_result=urlsplit(url)
  3.  
  4. print(split_result)
  5. print('split.path :', split_result.path)
  6. # SplitResult 沒有 params 屬性

結(jié)果為:

  1. SplitResult(scheme='http', netloc='user:pwd@domain:80', path='/path1;params1/path2;params2', query='query=queryarg', fragment='fragment')
  2. split.path : /path1;params1/path2;params2

若只是要將 URL 后的 fragment 標(biāo)識拆分出來,可以使用 urldefrag() 函數(shù):

  1. from urllib.parse import urldefrag
  2.  
  3. url = 'http://user:pwd@domain:80/path1;params1/path2;params2?query=queryarg#fragment'
  4.  
  5. d = urldefrag(url)
  6. print(d)
  7. print('url :', d.url)
  8. print('fragment:', d.fragment)

結(jié)果為:

  1. DefragResult(url='http://user:pwd@domain:80/path1;params1/path2;params2?query=queryarg', fragment='fragment')
  2. url : http://user:pwd@domain:80/path1;params1/path2;params2?query=queryarg
  3. fragment: fragment 

組建URL

ParsedResult 對象和 SplitResult 對象都有一個 geturl() 方法,可以返回一個完整的 URL 字符串。

  1. print(parsed_result.geturl())
  2. print(split_result.geturl())

結(jié)果為:

  1. http://user:pwd@domain:80/path1;params1/path2;params2?query=queryarg#fragment
  2. http://user:pwd@domain:80/path1;params1/path2;params2?query=queryarg#fragment

但是 geturl() 只在 ParsedResultSplitResult 對象中有,若想將一個普通的元組組成 URL,則需要使用 urlunparse() 函數(shù):

  1. from urllib.parse import urlunparse
  2. url_compos = ('http', 'user:pwd@domain:80', '/path1;params1/path2', 'params2', 'query=queryarg', 'fragment')
  3. print(urlunparse(url_compos))

結(jié)果為:

  1. http://user:pwd@domain:80/path1;params1/path2;params2?query=queryarg#fragment 

相對路徑轉(zhuǎn)換絕對路徑

除此之外,urllib.parse 還提供了一個 urljoin() 函數(shù),來將相對路徑轉(zhuǎn)換成絕對路徑的 URL。

  1. from urllib.parse import urljoin
  2.  
  3. print(urljoin('http://www.example.com/path/file.html', 'anotherfile.html'))
  4. print(urljoin('http://www.example.com/path/', 'anotherfile.html'))
  5. print(urljoin('http://www.example.com/path/file.html', '../anotherfile.html'))
  6. print(urljoin('http://www.example.com/path/file.html', '/anotherfile.html'))

結(jié)果為:

  1. http://www.example.com/path/anotherfile.html
  2. http://www.example.com/path/anotherfile.html
  3. http://www.example.com/anotherfile.html
  4. http://www.example.com/anotherfile.html 

查詢參數(shù)的構(gòu)造和解析

使用 urlencode() 函數(shù)可以將一個 dict 轉(zhuǎn)換成合法的查詢參數(shù):

  1. from urllib.parse import urlencode
  2.  
  3. query_args = {
  4. 'name': 'dark sun',
  5. 'country': '中國'
  6. }
  7.  
  8. query_args = urlencode(query_args)
  9. print(query_args)

結(jié)果為:

  1. name=dark+sun&country=%E4%B8%AD%E5%9B%BD

可以看到特殊字符也被正確地轉(zhuǎn)義了。

相對的,可以使用 parse_qs() 來將查詢參數(shù)解析成 dict。

  1. from urllib.parse import parse_qs
  2. print(parse_qs(query_args))

結(jié)果為:

  1. {'name': ['dark sun'], 'country': ['中國']}

如果只是希望對特殊字符進(jìn)行轉(zhuǎn)義,那么可以使用 quote 或 quote_plus 函數(shù),其中 quote_plus 比 quote 更激進(jìn)一些,會把 :、/ 一類的符號也給轉(zhuǎn)義了。

  1. from urllib.parse import quote, quote_plus, urlencode
  2.  
  3. url = 'http://localhost:1080/~hello!/'
  4. print('urlencode :', urlencode({'url': url}))
  5. print('quote :', quote(url))
  6. print('quote_plus:', quote_plus(url))

結(jié)果為:

  1. urlencode : url=http%3A%2F%2Flocalhost%3A1080%2F%7Ehello%21%2F
  2. quote : http%3A//localhost%3A1080/%7Ehello%21/
  3. quote_plus: http%3A%2F%2Flocalhost%3A1080%2F%7Ehello%21%2F

可以看到 urlencode 中應(yīng)該是調(diào)用 quote_plus 來進(jìn)行轉(zhuǎn)義的。

逆向操作則使用 unquote 或 unquote_plus 函數(shù):

  1. from urllib.parse import unquote, unquote_plus
  2.  
  3. encoded_url = 'http%3A%2F%2Flocalhost%3A1080%2F%7Ehello%21%2F'
  4. print(unquote(encoded_url))
  5. print(unquote_plus(encoded_url))

結(jié)果為:

  1. http://localhost:1080/~hello!/
  2. http://localhost:1080/~hello!/

你會發(fā)現(xiàn) unquote 函數(shù)居然能正確地將 quote_plus 的結(jié)果轉(zhuǎn)換回來。 

責(zé)任編輯:龐桂玉 來源: Linux中國
相關(guān)推薦

2019-02-26 13:00:11

JavaScriptURL前端

2021-08-11 22:50:53

JavaScript編程開發(fā)

2022-11-08 11:49:09

NLP庫Python云服務(wù)

2009-12-10 16:04:56

PHP函數(shù)parse_

2019-07-25 09:15:36

Python參數(shù)命令

2023-05-09 08:18:07

Python開發(fā)技巧

2022-05-11 12:12:32

ScapyPython網(wǎng)絡(luò)包

2022-02-11 19:08:07

JavaScriptURLurlcat

2024-01-02 08:43:48

qs工具庫格式化

2021-07-05 12:09:58

Python編程語言

2010-03-15 10:49:57

Python函數(shù)變量

2011-08-18 13:37:57

iPhone項目靜態(tài)庫

2024-05-15 09:05:43

Python文檔處理工具自動化文檔生成

2014-03-13 13:44:37

BoltsParse底層庫

2021-01-12 05:03:15

PythonLxmlXpath

2022-01-13 11:41:55

URLDoSRCE

2013-10-31 10:59:41

Clouda使用

2010-08-11 13:28:46

Flex行為

2021-11-19 17:26:11

AppApplication方法

2021-11-23 09:09:27

Applicationandroid系統(tǒng)開發(fā)
點贊
收藏

51CTO技術(shù)棧公眾號