1. urlparse() 解析链接,注意,返回值比3多一个params的属性

from urllib.parse import urlparse

result = urlparse('http://www.baidu.com/index.html;user?id=5#comment')
print(type(result), result)
<class 'urllib.parse.ParseResult'>
ParseResult(scheme='http', netloc='www.baidu.com', path='/index.html', params='user', query='id=5', fragment='comment')

结果

2. urlunparse() 生成链接,数组必须要有6个元素

from urllib.parse import urlunparse

data = ['http', 'www.baidu.com', 'index.html', 'user', 'a=6', 'comment']
print(urlunparse(data))
http://www.baidu.com/index.html;user?a=6#comment

结果

3. urlsplit()  解析链接,一般用这个,因为网上的链接大多都没有params

from urllib.parse import urlsplit

result = urlsplit('http://www.baidu.com/index.html;user?id=5#comment')
print(result)
SplitResult(scheme='http', netloc='www.baidu.com', path='/index.html;user', query='id=5', fragment='comment')

结果

4. urlunsplit() 生成链接,数组中有且仅有5个值

from urllib.parse import urlunsplit

data = ['http', 'www.baidu.com', 'index.html', 'a=6', 'comment']
print(urlunsplit(data))
http://www.baidu.com/index.html?a=6#comment

结果

5. urljoin() 合并链接,

from urllib.parse import urljoin

print(urljoin('http://www.baidu.com', 'FAQ.html'))
print(urljoin('http://www.baidu.com', 'https://cuiqingcai.com/FAQ.html'))
print(urljoin('http://www.baidu.com/about.html', 'https://cuiqingcai.com/FAQ.html'))
print(urljoin('http://www.baidu.com/about.html', 'https://cuiqingcai.com/FAQ.html?question=2'))
print(urljoin('http://www.baidu.com?wd=abc', 'https://cuiqingcai.com/index.php'))
print(urljoin('http://www.baidu.com', '?category=2#comment'))
print(urljoin('www.baidu.com', '?category=2#comment'))
print(urljoin('www.baidu.com#comment', '?category=2'))
http://www.baidu.com/FAQ.html
https://cuiqingcai.com/FAQ.html
https://cuiqingcai.com/FAQ.html
https://cuiqingcai.com/FAQ.html?question=2
https://cuiqingcai.com/index.php
http://www.baidu.com?category=2#comment
www.baidu.com?category=#comment
www.baidu.com?category=

结果

6. urlencode() 参数序列化

from urllib.parse import urlencode

params = {
'name': 'germey',
'age':
}
base_url = 'http://www.baidu.com?'
url = base_url + urlencode(params)
print(url)
http://www.baidu.com?name=germey&age=22

结果

7. parse_qs() 反序列化

from urllib.parse import parse_qs

query = 'name=germey&age=22'
print(parse_qs(query))

这个结合1或者3非常实用的,怎么实用自行脑补。

{'name': ['germey'], 'age': ['']}

结果

8. parse_qsl() 将参数转化为元组组成的列

from urllib.parse import parse_qsl

query = 'name=germey&age=22'
print(parse_qsl(query))
[('name', 'germey'), ('age', '')]

结果

9. quote() 将内容转化为URL编码的格式,URL中带有中文参数时,请使用。

from urllib.parse import quote

keyword = '壁纸'
url = 'https://www.baidu.com/s?wd=' + quote(keyword)
print(url)
https://www.baidu.com/s?wd=%E5%A3%81%E7%BA%B8

结果

值得注意的是:只能用在参数部分,否则整个url都编码了,他的亲爹都不认识了。

10. unquote() 与9正好相反

from urllib.parse import unquote

url = 'https://www.baidu.com/s?wd=%E5%A3%81%E7%BA%B8'
print(unquote(url))
https://www.baidu.com/s?wd=壁纸

结果

参考自:https://cuiqingcai.com/5508.html

urllib.parse解析链接的更多相关文章

  1. urllib库:解析链接

    1from urllib.parse import urlparse, urlunparse, urlsplit, urlunsplit, urljoin, urlencode, parse_qs,  ...

  2. Python 的 urllib.parse 库解析 URL

      Python 中的 urllib.parse 模块提供了很多解析和组建 URL 的函数. 解析url urlparse() 函数可以将 URL 解析成 ParseResult 对象.对象中包含了六 ...

  3. URL组成成分及各部分作用简介及urllib.parse / uri

    URL的一般格式为(带方括号[]的为可选项): protocol :// hostname[:port] / path / [;parameters][?query]#fragment urllib. ...

  4. (转)Python3 模块3之 Urllib之 urllib.parse、urllib.robotparser

    原文:https://blog.csdn.net/qq_36148847/article/details/79153738 https://blog.csdn.net/zly412934578/art ...

  5. python爬虫之解析链接

    解析链接 1. urlparse() & urlunparse() urlparse() 是对url链接识别和分段的,API用法如下: urllib.parse.urlparse(urlstr ...

  6. 我与python3擦肩而过(三)—— 我去。。又是编码问题——urllib.parse.unquote

    记得初学python时就学的爬虫,经常遇到编码问题(其实在python3里面编码问题已经很少了...),用requests库就挺方便解决这些问题的.近来有共同学习python的程序员写了个电子书网站, ...

  7. urllib.parse

    1 url分解 import urllib.parse result = urllib.parse.urlparse('http://www.baidu.com') print(result) 结果为 ...

  8. urllib.parse.parse_qsl 的一个小问题

    最近在使用urllib时发现的一个问题,记录一下. 首先请分别执行下面这两句代码: 1."你好".encode("utf8").decode("gbk ...

  9. urllib url解析学习

    #!/usr/bin/env python # encoding: utf-8 from urllib.parse import * #urlparse:解析url分段 #urlsplit:类似url ...

随机推荐

  1. VirtualStringTree常用类和属性

    重要的类:TBaseVirtualTree = class(TCustomControl)TCustomVirtualStringTree = class(TBaseVirtualTree)TVirt ...

  2. Ubuntu 中查找软件安装的位置

    Ubuntu 中查找软件安装的位置 执行该程序 直接执行该程序,有时候一些程序执行时会显示出自己的位置,比如: 用命令 ps -e 找到该程序的名字 用 find 或 whereis 命令查找文件位置 ...

  3. javascript基础总汇

    ## javaScript是什么:1.JavaScript 运行在客户端(浏览器)的编程语言2.用来给HTML网页增加动态功能3.用来给HTML网页增加动态功能.4.Netscape在最初将其脚本语言 ...

  4. 你的package包名有问题!

    今天在Eclipse中运行我的Java程序中,就弹出了以下消息的窗口: 注意窗口的名字为Java Virtual Machine Launcher . Error : A JNI error has ...

  5. Docker镜像恢复与迁移

    首先我们先删除掉 mycentos_new:1.1 镜像(注意先停止并删除所有引用了的容器) docker rmi mycentos_new:1.1 然后执行此命令进行恢复 mycentos_new: ...

  6. Angular 一个简单的指令实现 阻止事件扩散

    //指令定义 @Directive({ selector: `click-stop-propagation` events: 'stopClick($event)' }) class ClickSto ...

  7. whetstone

    https://www.cnblogs.com/findumars/p/4173040.html 下载源码:http://www.netlib.org/benchmark/whetstone.c ar ...

  8. php内置函数分析之strtoupper()、strtolower()

    strtoupper(): PHP_FUNCTION(strtoupper) { zend_string *str; ZEND_PARSE_PARAMETERS_START(, ) Z_PARAM_S ...

  9. 035:DTL常用过滤器(4)

    join过滤器: 类似与 Python 中的 join ,将列表/元组/字符串用指定的字符进行拼接.示例代码如下: {{ value|join:"/" }} 如果 value 是等 ...

  10. 玩转MaxCompute studio SQL编辑器

    SQL因其简单易学的特点,是用户与MaxCompute服务交互的主要手段.如何帮助用户高效愉快的编写SQL是MaxCompute studio的核心使命,下面就让我们来一探究竟: 忘记语法 相信大家都 ...