日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

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

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
使用Python的urlliib.parse庫解析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è)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,禹城網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。

解析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 進一步進行拆分。

 
 
 
 
  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 進行拆分,所不同的是, urlsplit() 并不會把 路徑參數(shù)(params) 從 路徑(path) 中分離出來。

當 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 標識拆分出來,可以使用 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': ['中國']}

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


文章標題:使用Python的urlliib.parse庫解析URL
網(wǎng)址分享:http://www.dlmjj.cn/article/djedhie.html