日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
使用Python的urlliib.parse庫(kù)解析URL

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

成都創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),禹城企業(yè)網(wǎng)站建設(shè),禹城品牌網(wǎng)站建設(shè),網(wǎng)站定制,禹城網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,禹城網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

解析url

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

  • 協(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),'個(gè)元素')
  8. print(parsed_result)

結(jié)果為:

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

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

為了方便起見, ParseResult 還提供了 username、 password、 hostname、 port 對(duì) 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() 之外,還有一個(gè)類似的 urlsplit() 函數(shù)也能對(duì) URL 進(jìn)行拆分,所不同的是, urlsplit() 并不會(huì)把 路徑參數(shù)(params) 從 路徑(path) 中分離出來。

當(dāng) URL 中路徑部分包含多個(gè)參數(shù)時(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

這時(shí)可以使用 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)識(shí)拆分出來,可以使用 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 對(duì)象和 SplitResult 對(duì)象都有一個(gè) geturl() 方法,可以返回一個(gè)完整的 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 對(duì)象中有,若想將一個(gè)普通的元組組成 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 

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

除此之外,urllib.parse 還提供了一個(gè) urljoin() 函數(shù),來將相對(duì)路徑轉(zhuǎn)換成絕對(duì)路徑的 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ù)可以將一個(gè) dict 轉(zhuǎn)換成合法的查詢參數(shù):

 
 
 
 
  1. from urllib.parse import urlencode
  2.  
  3. query_args = {
  4. 'name': 'dark sun',
  5. 'country': '中國(guó)'
  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)義了。

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

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

結(jié)果為:

 
 
 
 
  1. {'name': ['dark sun'], 'country': ['中國(guó)']}

如果只是希望對(duì)特殊字符進(jìn)行轉(zhuǎn)義,那么可以使用 quote 或 quote_plus 函數(shù),其中 quote_plus 比 quote 更激進(jìn)一些,會(huì)把 :、/ 一類的符號(hào)也給轉(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!/

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


標(biāo)題名稱:使用Python的urlliib.parse庫(kù)解析URL
鏈接地址:http://www.5511xx.com/article/djedhie.html