日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

python爬虫第二课:url解析

發布時間:2025/3/15 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python爬虫第二课:url解析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

實例域名: http://www.baidu.com/index.html;user?id=S#comment
URL通過特定的分隔符解析:
冒號’:‘前面的就是 scheme,代表協議;第一個’/‘符號前面便是 netloc ,即域名,后面是 path,即訪 問路徑(其實包含‘/’本身);分號’;‘后面是 params ,代表參數;問號’?‘后面是查詢條件 query, 一般用作 GET 類型的 URL; 井號’#'后面是錨點,用于直接定位頁面內部的下拉位置。

urllib.parse.urlparse(urlstring, scheme=”, allow_fragments=True)
scheme: 它是默認的協議(比如 http 或 https 等) 假如這個鏈接沒有帶協議信息,會將這個作為默認的協議。
allow_fragments :即是否忽略fragment。 如果它被設置為 False,干ragment部分就會被忽略, 它會被解析為 path、 parameters 或者 query 的一部分,而 fragment 部分為空。
返回一個元組,我們可以用索引順序來獲取,也可以用屬性名獲取

實例代碼:

from urllib.parse import *url = 'http://www.baidu.com/index.html;user?id=5#comment' result1 = urlparse(url) print(type(result1)) print('result1 : ', result1) result1 = urlparse(url, allow_fragments=False) print(result1)# urlsplit() :和 urlparse()方法非常相似, 只不過它不再單獨解析 params 這一部分,只返回 5 個結果。同樣返回也是元組。 result2 = urlsplit(url) print('result2 : ', result2)# urlunparse():接受一個可迭代對象(字典取得是key部分,賊傻),長度必須是6.組裝為一個URL。 data1 = ['http', 'www.baidu.com', '/index.html', 'user', 'id=S', '#comment', ] print(urlunparse(data1))# urlunsplit() :與 urlunparse()類似,傳入的參數也是一個可迭代對象 ,唯一的區別是長度必須為 5。 data2 = ['http', 'www.baidu.com', '/index.html;user', 'id=S', '#comment', ] print(urlunsplit(data2))# urljoin() :提供一個 base_url (基礎鏈 接 ) 作為第一個參數,將新的鏈接作為第二個參數,該方法會分析 base_url 的 scheme、 netloc # 和 path 這 3 個內容并對新鏈接缺失的部分進行補充,最后返回結果。 print(urljoin('http://www.baidu.com', '?category=2#comment'))# urlencode() :將一個字典構造為 GET 請求參數 params = {'scheme': 'http', 'netloc': 'www.baidu.com', 'path': '/index.html', 'name': 'germey', 'age': 22} print(urlencode(params)) params = {'name': 'germey', 'age': 22} print('http://www.baidu.com?' + urlencode(params)) #所以urlencode只是加參數,并不負責解析字典# parse_qs():將參數轉化字典 # parse_qsl():將參數轉化為元組組成的列表 print(parse_qs('name=germey&age=22')) print(parse_qsl('name=germey&age=22'))# quote() :URL 中帶有中文參數時,有時可能會導致亂碼的問題,用這個方法可以將文字符轉化為 URL 編碼。 # unquote() :它可以進行 URL解碼。 keyword = '壁紙' url = 'https://www.baidu.com/s?wd='+ quote(keyword) print(url) print(unquote(url))

程序運行結果:

<class 'urllib.parse.ParseResult'> result1 : ParseResult(scheme='http', netloc='www.baidu.com', path='/index.html', params='user', query='id=5', fragment='comment') ParseResult(scheme='http', netloc='www.baidu.com', path='/index.html', params='user', query='id=5#comment', fragment='') result2 : SplitResult(scheme='http', netloc='www.baidu.com', path='/index.html;user', query='id=5', fragment='comment') http://www.baidu.com/index.html;user?id=S##comment http://www.baidu.com/index.html;user?id=S##comment http://www.baidu.com?category=2#comment scheme=http&netloc=www.baidu.com&path=%2Findex.html&name=germey&age=22 http://www.baidu.com?name=germey&age=22 {'name': ['germey'], 'age': ['22']} [('name', 'germey'), ('age', '22')] https://www.baidu.com/s?wd=%E5%A3%81%E7%BA%B8 https://www.baidu.com/s?wd=壁紙

總結

以上是生活随笔為你收集整理的python爬虫第二课:url解析的全部內容,希望文章能夠幫你解決所遇到的問題。

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