• urlparse模块


    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基地址后面没有/则该处会被替换掉

  • 相关阅读:
    ngnix+uwsgi+django 部署mezzanine
    shell三剑客之find
    Flask常见面试问题
    redis宕机如何解决?如果是项目上线的宕机呢?
    UiPath,容智Ibot在线接单,有需求的欢迎过来
    CORS和CSRF
    JWT黑名单和白名单
    Django项目常见面试问题
    降低Redis内存占用
    Redis-缓存有效期与淘汰策略
  • 原文地址:https://www.cnblogs.com/chillytao-suiyuan/p/9829496.html
Copyright © 2020-2023  润新知