日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

urllib.parse包学习

發布時間:2025/4/5 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 urllib.parse包学习 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、前言

我是在進行全站爬取某個網站時用到的這個包,它的主要功能就是分解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)轉化成絕對地址。

3、函數介紹

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'

這六個部分的解釋

AttributeIndexValueValue if not present
scheme0URL scheme specifier(也就是http/https)scheme parameter
netloc1Network location part(域名)empty string
path2Hierarchical path(分層路徑)empty string
params3Parameters for last path element(最后一個路徑元素的參數)empty string
query4Query component(查詢組件)empty string
fragment5Fragment identifier(片段識別)empty string

函數方法說明
urlstring : URL路徑
scheme : 協議類型,http或者https
allow_fragments: 默認是True,如果設置為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.

更多關于urllib.parse的內容可前往官網

總結

以上是生活随笔為你收集整理的urllib.parse包学习的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。