码迷,mamicode.com
首页 > Web开发 > 详细

urlparse模块

时间:2018-10-22 14:59:14      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:info   图片   for   parse   一个   thunder   字符串   操作   div   

urlparse模块中为操作URL字符串提供了3种方法:

urlparse()

urlunparse()

urljoin()

1.urlparse()方法主要将URL字符串拆分成一个6元素元组

 

>>> from urllib.parse import urlparse
           
>>> url = "http://www.cnblogs.com/thunderLL/p/6643022.html?pid=‘8766352‘"
           
>>> url_turple = urlparse(url)
           
>>> for i,each in enumerate(url_turple):
           print(i,each)

           
0 http
1 www.cnblogs.com
2 /thunderLL/p/6643022.html
3 
4 pid=8766352
5 
>>> 

 

技术分享图片

2.urlunparse()方法主要将URL的6元素元组变成url路径;与urlparse方法作用相反

>>> from urllib.parse import urlunparse

 >>> path = urlunparse((‘http‘,‘www.cnblogs.com‘,‘/thunderLL/p/6643022.html‘,‘‘,‘pid=8766352‘,‘‘))

 >>> path

 ‘http://www.cnblogs.com/thunderLL/p/6643022.html?pid=8766352‘

3.urljoin()方法

技术分享图片

>>> from urllib.parse import urljoin
           
>>> url1 = urljoin(http://www.baidu.com/admin/,module-urllib2/request-objects.html)
           
>>> url1
           
http://www.baidu.com/admin/module-urllib2/request-objects.html
>>> url2 = urljoin(http://www.baidu.com/admin,module-urllib2/request-objects.html)
           
>>> url2
           
http://www.baidu.com/module-urllib2/request-objects.html

urljoin()方法拼接两个URL(基地址和相对地址)得到的地址url1和url2,这两个URL区别在于基地址后面有无/,导致的运行结果存在差异

URL基地址后面没有/则该处会被替换掉

 

urlparse模块

标签:info   图片   for   parse   一个   thunder   字符串   操作   div   

原文地址:https://www.cnblogs.com/chillytao-suiyuan/p/9829496.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!