python——urlparse:解析url


 urlparse模塊主要是把url拆分為6部分,並返回元組。並且可以把拆分后的部分再組成一個url。主要有函數有urljoin、urlsplit、urlunsplit、urlparse、parse_qs等。

urlparse.urlparse(urlstring[, scheme[,allow_fragments]])

      將urlstring解析成6個部分,它從urlstring中取得URL,並返回元組 (scheme, netloc, path, parameters, query, fragment),但是實際上是基於namedtuple,是tuple的子類。它支持通過名字屬性或者索引訪問的部分URL,每個組件是一串字符,也有可能是空的。組件不能被解析為更小的部分,%后面的也不會被解析,分割符號並不是解析結果的一部分,除非用斜線轉義,注意,返回的這個元組非常有用,例如可以用來確定網絡協議(HTTP、FTP等等 )、服務器地址、文件路徑,等等。

>>> import urlparse
>>> parsed_tuple = urlparse.urlparse("http://www.google.com/search?hl=en&q=urlparse&btnG=Google+Search")
>>> print parsed_tuple
ParseResult(scheme='http', netloc='www.google.com', path='/search', params='', query='hl=en&q=urlparse&btnG=Google+Search', fragment='')

urlparse.urlunparse(parts)

    從一個元組構建一個url,元組類似urlparse返回的,它接收元組(scheme, netloc, path, parameters, query, fragment)后,會重新組成一個具有正確格式的URL,以便供Python的其他HTML解析模塊使用。

>>> import urlparse
>>> parsed_tuple = urlparse.urlparse("http://www.google.com/search?hl=en&q=urlparse&btnG=Google+Search")
>>> print parsed_tuple
ParseResult(scheme='http', netloc='www.google.com', path='/search', params='', query='hl=en&q=urlparse&btnG=Google+Search', fragment='')
>>> url=urlparse.urlunparse(parsed_tuple)
>>> print url
http://www.google.com/search?hl=en&q=urlparse&btnG=Google+Search

urlparse.urlsplit(urlstring[, scheme[, allow_fragments]])

       主要是分析urlstring,返回一個包含5個字符串項目的元組:協議、位置、路徑、查詢、片段。allow_fragments為False時,該元組的組后一個項目總是空,不管urlstring有沒有片段,省略項目的也是空。urlsplit()和urlparse()差不多。不過它不切分URL的參數。適用於遵循RFC2396的URL,每個路徑段都支持參數。這樣返回的元組就只有5個元素。

>>> split_tuple = urlparse.urlsplit("http://www.google.com/search?hl=en&q=urlparse&btnG=Google+Search")
>>> print split_tuple
SplitResult(scheme='http', netloc='www.google.com', path='/search', query='hl=en&q=urlparse&btnG=Google+Search', fragment='')
>>> parsed_tuple = urlparse.urlparse("http://www.google.com/search?hl=en&q=urlparse&btnG=Google+Search")
>>> print parsed_tuple
ParseResult(scheme='http', netloc='www.google.com', path='/search', params='', query='hl=en&q=urlparse&btnG=Google+Search', fragment='')

urlparse.urlunsplit(parts)

    urlunsplit使用urlsplit()返回的值組合成一個url

>>> split_tuple = urlparse.urlsplit("http://www.google.com/search?hl=en&q=urlparse&btnG=Google+Search")
>>> print split_tuple
SplitResult(scheme='http', netloc='www.google.com', path='/search', query='hl=en&q=urlparse&btnG=Google+Search', fragment='')
>>> url=urlparse.urlunsplit(split_tuple)
>>> print url
http://www.google.com/search?hl=en&q=urlparse&btnG=Google+Search

urlparse.urljoin(base, url[, allow_fragments])

     urljoin主要是拼接URL,它以base作為其基地址,然后與url中的相對地址相結合組成一個絕對URL地址。函數urljoin在通過為URL基地址附加新的文件名的方式來處理同一位置處的若干文件的時候格外有用。需要注意的是,如果基地址並非以字符/結尾的話,那么URL基地址最右邊部分就會被這個相對路徑所替換。如果希望在該路徑中保留末端目錄,應確保URL基地址以字符/結尾。

>>> import urlparse
>>> urlparse.urljoin('http://www.google.com/search?','hl=en&q=urlparse')
'http://www.google.com/hl=en&q=urlparse'
>>> urlparse.urljoin('http://www.google.com/search?/','hl=en&q=urlparse')
'http://www.google.com/hl=en&q=urlparse'
>>> urlparse.urljoin('http://www.google.com/search/','hl=en&q=urlparse')
'http://www.google.com/search/hl=en&q=urlparse'

parse_qs(qskeep_blank_values=Falsestrict_parsing=Falseencoding='utf-8'errors='replace')

 解析query,返回詞典格式數據,詞典的key是query中變量名字,value是對應的值。

參數keep_blank_value標識空值是否識別為一個空字符串。true——空值應該識別為一個空字符串,false(默認值)不把空值識別為字符串。

strict_parsing:標識解析失敗的時候怎么處理,false(默認值)——忽略失敗情況;true——拋出ValueError異常;

encoding、errors:指出如何將參數解碼為Unicode字符串。

>>> import urlparse
>>> parsed_tuple = urlparse.urlparse("http://www.google.com/search?hl=en&q=urlparse&btnG=")
>>> print parsed_tuple
ParseResult(scheme='http', netloc='www.google.com', path='/search', params='', query='hl=en&q=urlparse&btnG=', fragment='')
>>> urlparse.parse_qs(parsed_tuple.query)
{'q': ['urlparse'], 'hl': ['en']}
>>> urlparse.parse_qs(parsed_tuple.query, True)
{'q': ['urlparse'], 'btnG': [''], 'hl': ['en']}

 

參考:

https://my.oschina.net/guol/blog/95699

http://blog.sina.com.cn/s/blog_5ff7f94f0100qr3c.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM