zoukankan      html  css  js  c++  java
  • python——urlparse:解析url

     urlparse模块主要是把url拆分为6部分,并返回元组。并且可以把拆分后的部分再组成一个url。主要有函数有urljoin、urlsplit、urlunsplit、urlparse、parse_qs等。

    urlparse.urlparse(urlstring[, scheme[,allow_fragments]])

          将urlstring解析成6个部分,它从urlstring中取得URL,并返回元组 (scheme, netloc, path, parameters, query, fragment),但是实际上是基于namedtuple,是tuple的子类。它支持通过名字属性或者索引访问的部分URL,每个组件是一串字符,也有可能是空的。组件不能被解析为更小的部分,%后面的也不会被解析,分割符号并不是解析结果的一部分,除非用斜线转义,注意,返回的这个元组非常有用,例如可以用来确定网络协议(HTTP、FTP等等 )、服务器地址、文件路径,等等。

    >>> import urlparse
    >>> parsed_tuple = urlparse.urlparse("http://www.google.com/search?hl=en&q=urlparse&btnG=Google+Search")
    >>> print parsed_tuple
    ParseResult(scheme='http', netloc='www.google.com', path='/search', params='', query='hl=en&q=urlparse&btnG=Google+Search', fragment='')

    urlparse.urlunparse(parts)

        从一个元组构建一个url,元组类似urlparse返回的,它接收元组(scheme, netloc, path, parameters, query, fragment)后,会重新组成一个具有正确格式的URL,以便供Python的其他HTML解析模块使用。

    >>> import urlparse
    >>> parsed_tuple = urlparse.urlparse("http://www.google.com/search?hl=en&q=urlparse&btnG=Google+Search")
    >>> print parsed_tuple
    ParseResult(scheme='http', netloc='www.google.com', path='/search', params='', query='hl=en&q=urlparse&btnG=Google+Search', fragment='')
    >>> url=urlparse.urlunparse(parsed_tuple)
    >>> print url
    http://www.google.com/search?hl=en&q=urlparse&btnG=Google+Search

    urlparse.urlsplit(urlstring[, scheme[, allow_fragments]])

           主要是分析urlstring,返回一个包含5个字符串项目的元组:协议、位置、路径、查询、片段。allow_fragments为False时,该元组的组后一个项目总是空,不管urlstring有没有片段,省略项目的也是空。urlsplit()和urlparse()差不多。不过它不切分URL的参数。适用于遵循RFC2396的URL,每个路径段都支持参数。这样返回的元组就只有5个元素。

    >>> split_tuple = urlparse.urlsplit("http://www.google.com/search?hl=en&q=urlparse&btnG=Google+Search")
    >>> print split_tuple
    SplitResult(scheme='http', netloc='www.google.com', path='/search', query='hl=en&q=urlparse&btnG=Google+Search', fragment='')
    >>> parsed_tuple = urlparse.urlparse("http://www.google.com/search?hl=en&q=urlparse&btnG=Google+Search")
    >>> print parsed_tuple
    ParseResult(scheme='http', netloc='www.google.com', path='/search', params='', query='hl=en&q=urlparse&btnG=Google+Search', fragment='')

    urlparse.urlunsplit(parts)

        urlunsplit使用urlsplit()返回的值组合成一个url

    >>> split_tuple = urlparse.urlsplit("http://www.google.com/search?hl=en&q=urlparse&btnG=Google+Search")
    >>> print split_tuple
    SplitResult(scheme='http', netloc='www.google.com', path='/search', query='hl=en&q=urlparse&btnG=Google+Search', fragment='')
    >>> url=urlparse.urlunsplit(split_tuple)
    >>> print url
    http://www.google.com/search?hl=en&q=urlparse&btnG=Google+Search

    urlparse.urljoin(base, url[, allow_fragments])

         urljoin主要是拼接URL,它以base作为其基地址,然后与url中的相对地址相结合组成一个绝对URL地址。函数urljoin在通过为URL基地址附加新的文件名的方式来处理同一位置处的若干文件的时候格外有用。需要注意的是,如果基地址并非以字符/结尾的话,那么URL基地址最右边部分就会被这个相对路径所替换。如果希望在该路径中保留末端目录,应确保URL基地址以字符/结尾。

    >>> import urlparse
    >>> urlparse.urljoin('http://www.google.com/search?','hl=en&q=urlparse')
    'http://www.google.com/hl=en&q=urlparse'
    >>> urlparse.urljoin('http://www.google.com/search?/','hl=en&q=urlparse')
    'http://www.google.com/hl=en&q=urlparse'
    >>> urlparse.urljoin('http://www.google.com/search/','hl=en&q=urlparse')
    'http://www.google.com/search/hl=en&q=urlparse'

    parse_qs(qskeep_blank_values=Falsestrict_parsing=Falseencoding='utf-8'errors='replace')

     解析query,返回词典格式数据,词典的key是query中变量名字,value是对应的值。

    参数keep_blank_value标识空值是否识别为一个空字符串。true——空值应该识别为一个空字符串,false(默认值)不把空值识别为字符串。

    strict_parsing:标识解析失败的时候怎么处理,false(默认值)——忽略失败情况;true——抛出ValueError异常;

    encoding、errors:指出如何将参数解码为Unicode字符串。

    >>> import urlparse
    >>> parsed_tuple = urlparse.urlparse("http://www.google.com/search?hl=en&q=urlparse&btnG=")
    >>> print parsed_tuple
    ParseResult(scheme='http', netloc='www.google.com', path='/search', params='', query='hl=en&q=urlparse&btnG=', fragment='')
    >>> urlparse.parse_qs(parsed_tuple.query)
    {'q': ['urlparse'], 'hl': ['en']}
    >>> urlparse.parse_qs(parsed_tuple.query, True)
    {'q': ['urlparse'], 'btnG': [''], 'hl': ['en']}

     

    参考:

    https://my.oschina.net/guol/blog/95699

    http://blog.sina.com.cn/s/blog_5ff7f94f0100qr3c.html

  • 相关阅读:
    2016.6.23 随笔———— AJAX
    2016.6.13 随笔————图像获取、处理,视频获取,png图片尺寸缩小
    2016.5.15 随笔————网页平面设计软件 Illustrator(Ai) 和 Photoshop(Ps) 简介
    学习的目的:理解<转>
    几点要求自己也可以借鉴
    手表电池
    许小年:宁可踏空,不可断粮<转>
    【微言大义】时间都去哪了?
    互联网趋势其实很浮夸
    解决Mac下GDB提示签名错误
  • 原文地址:https://www.cnblogs.com/myyan/p/7149542.html
Copyright © 2011-2022 走看看