urllib.parse包学习
1、前言
我是在進行全站爬取某個網(wǎng)站時用到的這個包,它的主要功能就是分解URL,在對URL處理時是一個非常有用的包
2、功能介紹
This module defines a standard interface to break Uniform Resource Locator (URL) strings up in components (addressing scheme, network location, path etc.), to combine the components back into a URL string, and to convert a “relative URL” to an absolute URL given a “base URL.”
這組模塊(即urllib.parse包)定義了一個標準接口,用于將URL分解成一個一個個組件,將組件重新組建成一個URL字符串。也就是利用基本的URL將相對地址(URL)轉(zhuǎn)化成絕對地址。
3、函數(shù)介紹
3.1、URL Parsing
The URL parsing functions focus on splitting a URL string into its components, or on combining URL components into a URL string.
3.1.1、urllib.parse.urlparse(urlstring, scheme=”, allow_fragments=True)
urlparse()會將URL分解成六個部分,看例子
>>> from urllib.parse import urlparse >>> o = urlparse('http://www.cwi.nl:80/%7Eguido/Python.html') >>> o ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',params='', query='', fragment='') >>> o.scheme 'http' >>> o.port 80 >>> o.geturl() 'http://www.cwi.nl:80/%7Eguido/Python.html'這六個部分的解釋
| scheme | 0 | URL scheme specifier(也就是http/https) | scheme parameter |
| netloc | 1 | Network location part(域名) | empty string |
| path | 2 | Hierarchical path(分層路徑) | empty string |
| params | 3 | Parameters for last path element(最后一個路徑元素的參數(shù)) | empty string |
| query | 4 | Query component(查詢組件) | empty string |
| fragment | 5 | Fragment identifier(片段識別) | empty string |
函數(shù)方法說明
urlstring : URL路徑
scheme : 協(xié)議類型,http或者https
allow_fragments: 默認是True,如果設(shè)置為False,fragment identifiers將不會被識別,就是說netloc后面的都會當成URL中的路徑處理。
If the allow_fragments argument is false, fragment identifiers are not recognized. Instead, they are parsed as part of the path, parameters or query component, and fragment is set to the empty string in the return value.
更多關(guān)于urllib.parse的內(nèi)容可前往官網(wǎng)
總結(jié)
以上是生活随笔為你收集整理的urllib.parse包学习的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python学习之GUI--SQL数据库
- 下一篇: StratifiedKFold()与KF