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. 问题 A: 组合数

    问题 A: 组合数 时间限制: 1 Sec  内存限制: 128 MB提交: 1975  解决: 150[提交] [状态] [命题人:jsu_admin] 题目描述 求组合数C(N,M),以及C(N, ...

  2. jQuery淡入淡出瀑布流效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. 回溯---N皇后

    N 皇后 51. N-Queens (Hard) 题目描述:   在n*n的矩阵中摆放n个皇后,并且每个皇后不能在同一列,同一个对角线上,求所有的n皇后解. 思路分析:   一行一行地摆放,在确定一行 ...

  4. zTree根节点默认打开

    1.在生成tree的json数据中,直接给出:open:true的属性; 2.treeObj.expandAll(true); 3.var zTree = $.fn.zTree.getZTreeObj ...

  5. referenceQueue用法

    何为referenceQueue 在java的引用体系中,存在着强引用,软引用,虚引用,幽灵引用,这4种引用类型.在正常的使用过程中,我们定义的类型都是强引用的,这种引用类型在回收中,只有当其它对象没 ...

  6. leetcode 003

    3. Longest Substring Repeating Character Difficulty:Medium The link: https://leetcode.com/problems/l ...

  7. CSS3 ::before和::after伪元素的实际应用

    实例 1.清除浮动 通常我们清除清除浮动的方式就是在浮动元素后面添加一个空的Div标签,然后在设置它的清除浮动要是,使用after伪元素,我们就不需要添加无意义的div标签在html中了,下面的例子就 ...

  8. 1N4148

    摘自http://baike.baidu.com/link?url=0iTO7zZvHpCeJiZurTPpjDT95YdJu7cKdTeCWfol36b4JG5ii15leQ7K4wJWAZIBNb ...

  9. 对vue的api的研究

    Vue.config 是一个对象,包含 Vue 的全局配置.可以在启动应用之前修改下列属性: silent 类型:boolean 默认值:false 用法: Vue.config.silent = t ...

  10. 小米笔记本pro版bios经常找不到硬盘

    自从买了小米笔记本,对小米的印象大大折扣,bios经常找不到硬盘,关机,重启,就好了. 到小米售后,售后说是系统坏了,我说bios里都找不到.他说,系统坏了也会出现这个情况.我说好吧.重做后,没用几天 ...