urllib.parse解析链接
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
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解析链接的更多相关文章
- urllib库:解析链接
1from urllib.parse import urlparse, urlunparse, urlsplit, urlunsplit, urljoin, urlencode, parse_qs, ...
- Python 的 urllib.parse 库解析 URL
Python 中的 urllib.parse 模块提供了很多解析和组建 URL 的函数. 解析url urlparse() 函数可以将 URL 解析成 ParseResult 对象.对象中包含了六 ...
- URL组成成分及各部分作用简介及urllib.parse / uri
URL的一般格式为(带方括号[]的为可选项): protocol :// hostname[:port] / path / [;parameters][?query]#fragment urllib. ...
- (转)Python3 模块3之 Urllib之 urllib.parse、urllib.robotparser
原文:https://blog.csdn.net/qq_36148847/article/details/79153738 https://blog.csdn.net/zly412934578/art ...
- python爬虫之解析链接
解析链接 1. urlparse() & urlunparse() urlparse() 是对url链接识别和分段的,API用法如下: urllib.parse.urlparse(urlstr ...
- 我与python3擦肩而过(三)—— 我去。。又是编码问题——urllib.parse.unquote
记得初学python时就学的爬虫,经常遇到编码问题(其实在python3里面编码问题已经很少了...),用requests库就挺方便解决这些问题的.近来有共同学习python的程序员写了个电子书网站, ...
- urllib.parse
1 url分解 import urllib.parse result = urllib.parse.urlparse('http://www.baidu.com') print(result) 结果为 ...
- urllib.parse.parse_qsl 的一个小问题
最近在使用urllib时发现的一个问题,记录一下. 首先请分别执行下面这两句代码: 1."你好".encode("utf8").decode("gbk ...
- urllib url解析学习
#!/usr/bin/env python # encoding: utf-8 from urllib.parse import * #urlparse:解析url分段 #urlsplit:类似url ...
随机推荐
- CVE-2016-2502-drivers/usb/gadget/f_serial.c in the Qualcomm USB driver in Android. Buffer Overflow Vulnerability reported by #plzdonthackme, Soctt.
CVE-2016-2502-drivers/usb/gadget/f_serial.c in the Qualcomm USB driver in Android.Buffer Overflow Vu ...
- 在react中用装饰器来装饰connect
假设我们在react中有如下header组件: import React, { PureComponent } from 'react'; import { connect } from 'react ...
- hdu6357 Hills And Valleys (最长不下降子序列)
题目传送门 题意: 给你0~9的字符串,问你翻转哪个区间后使得其最长不下降子序列长度最长 思路: 因为字符是0~9,所以我们可以定义一个b数组来枚举L,R, 去和原来的字符串去求最长公共子序列长度,不 ...
- PowerDesigner 使用说明
1. 附加:工具栏不见了 调色板(Palette)快捷工具栏不见了PowerDesigner 快捷工具栏 palette 不见了,怎么重新打开,找回来呢 上网搜索了一下"powerdesig ...
- vue css中scoped
1.什么是scoped vue组件中,在style标签中有一个属性,叫做scoped.当此标签拥有scoped属性的时候,该组件下的css样式只适用于本组件,而不会影响全局组件.这其实也相当于样式的模 ...
- C++基础之static(静态)变量
static 表示静态 作用: 1.在函数体内,静态变量的值维持不变(记忆功能) 2.是一个本地的全局函数,即只能被本模块的函数访问(隐藏功能) static变量: static全局变量和普通 ...
- 认知redis
一.redis是什么? 1.基于key-value的内存No sql 数据库(非关系型数据库) 2.读写性能非常好 二.redisd的数据类型有哪些?特点分别是什么? 1)string 一个键对一个值 ...
- 比较两个Sql数据库是否相同
1.打开VS20122.SQL→架构比较→新建架构比较3.在源和目标上分别填上两个待比较的数据库的信息4.点击比较,不一会儿,系统就会列出两个数据库的差异了.
- 树莓派 msmtp和mutt 的安装和配置
1,安装mutt sudo apt-get install mutt 2,安装msmtp sudo apt-get install msmtp 3,设置mutt /etc/Muttrc # 系统全局设 ...
- 初学Java 数组统计字母
public class CountLetterInArray { public static void main(String[] args) { char[] chars = createArra ...