python中url解析 or url的base64编码
目录
from urllib.parse import urlparse, quote, unquote, urlencode
1、解析url的组成成分:urlparse(url)
2、url的base64编解码:quote(url)、unquote(url)
3、字典变成一个字符串=&连接,并且被base64编码:urlencode(字典)
from urllib.parse import urlparse, quote, unquote, urlencode
print("======== 1 解析url的组成==========")
url='http://www.freebuf.com/articles/437.html'
url_parse = urlparse(url)
print(url_parse)
print(url_parse.netloc)
print("======== 2 对字符串进行base64编码和反编码==========")
url = 'http://www.example.com/api.php?text=中文在这里'
# 不带附加参数
print('不带附加参数:\n%s' % quote(url))
# 附带不转换字符参数
print('附加不转换字符参数:\n%s' % quote(url, safe='/:?='))
print(unquote("text%3D%E4%B8%AD%E6%96%87%E5%9C%A8%E8%BF%99%E9%87%8C"))
print("======== 3 对字典转换为=&连接的字符串,并且base64编码==========")
params = {
"uid": "123",
"type": "get",
"nickname": "abc划水的鱼儿",
}
print(params)
data_str_base64 = urlencode(params) # 字典转为字符串,并且base64编码
print(type(data_str_base64))
print(data_str_base64)
data_str_unbase64 = unquote(data_str_base64) # 字符串中base64被反解码
print(data_str_unbase64)
输出结果:

到此看懂了,后面就可以不用看了。
1、解析url的组成成分:urlparse(url)
提取域名domain
from urllib.parse import urlparse url = 'http://www.freebuf.com/articles/437.html'
url_parse = urlparse(url)
print(url_parse)
print(url_parse.netloc)
print(url_parse.hostname)
输出:
ParseResult(scheme='http', netloc='www.freebuf.com', path='/articles/437.html', params='', query='', fragment='')
www.freebuf.com
www.freebuf.com
2、url的base64编解码:quote(url)、unquote(url)
url的base64编码 、解码
from urllib.parse import quote
url = 'http://www.example.com/api.php?text=中文在这里' # 不带附加参数
print('\n不带附加参数:\n%s' % quote(url)) # 附带不转换字符参数
print('\n附加不转换字符参数:\n%s' % quote(url, safe='/:?='))
输出结果:

base64解码
print(unquote("text%3D%E4%B8%AD%E6%96%87%E5%9C%A8%E8%BF%99%E9%87%8C"))
(一)字符串转义为base64编码和解码(和上文重复,不用看)
import urllib.parse url_han = 'https://www.baidu.com/s?wd=北京'
print(urllib.parse.quote(url_han)) # base64编码
# https%3A//www.baidu.com/s%3Fwd%3D%E5%8C%97%E4%BA%AC url_base64 = 'https://www.baidu.com/s?wd=%E6%B7%B1%E5%9C%B3'
print(urllib.parse.unquote(url_base64)) # base64反编码
# https://www.baidu.com/s?wd=深圳
3、字典变成一个字符串=&连接,并且被base64编码
(二)字典转化为&连接的字符串
说明:字典转字符串后,发生了2件事
1、冒号变成等号;逗号变成&
2、汉字和特殊字符被base64编码
案例:
源代码:
import urllib.parse
params = {
"uid": "123",
"type": "get",
"nickname": "abc划水的鱼儿",
}
print(params)
data_str_base64 = urllib.parse.urlencode(params) # 字典转为字符串,并且base64编码
print(type(data_str_base64))
print(data_str_base64)
data_str_unbase64 = urllib.parse.unquote(data_str_base64) # 字符串中base64被反解码
print(data_str_unbase64)
输出结果:
{'uid': '123', 'type': 'get', 'nickname': 'abc划水的鱼儿'}
<class 'str'>
uid=123&type=get&nickname=abc%E5%88%92%E6%B0%B4%E7%9A%84%E9%B1%BC%E5%84%BF
uid=123&type=get&nickname=abc划水的鱼儿
python中url解析 or url的base64编码的更多相关文章
- python中html解析-Beautiful Soup
1. Beautiful Soup的简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: Beautiful Soup提供一些简单的.pyt ...
- python中unicode、utf8、gbk等编码问题
转自:http://luchanghong.com/python/2012/07/06/python-encoding-with-unicode-and-gbk-and-utf8.html 概要:编码 ...
- Python中xlwt解析
1.导入模块 import xlwt 2.构造excel表 workbook = xlwt.Workbook() #返回一个工作簿对象 3.构造sheet w ...
- python中html解析
import requestsfrom bs4 import BeautifulSoup url = "..." payload =...headers = None respon ...
- Python中配置文件解析模块-ConfigParser
Python中有ConfigParser类,可以很方便的从配置文件中读取数据(如DB的配置,路径的配置).配置文件的格式是: []包含的叫section, section 下有option=value ...
- Python中yield解析
小探yield 查看 python yield 文档 yield expressions: Using a yield expression in a function's body causes t ...
- Python中xlutils解析
1.导入模块 import xlrd import xlutils.copy 2.打开模块表 book = xlrd.open_workbook('test.xls', formatting_info ...
- Python中Json解析的坑
JSON虽好,一点点不对,能把人折腾死: 1.变量必须要用双引号 2.如果是字符串,必须要用引号包起来 Error:Expecting : delimiter: line 1 column 6 (ch ...
- python中xml解析
import xml.dom.minidom input_xml_string = '''<root><a>hello</a></root>'''#打开 ...
随机推荐
- [Ubuntu] geoip-bin 程序包 - 查询 IP 归属地
简述:在Linux命令行下查询IP归属地. 对Ubuntu/Debian系统,使用APT命令进行安装: $ sudo apt-get install geoip-bin 该包由MaxMind提供,它同 ...
- Python内置性能分析模块timeit
timeit模块 timeit模块可以用来测试一小段Python代码的执行速度. class timeit.Timer(stmt='pass', setup='pass', timer=<tim ...
- 【Spring Boot && Spring Cloud系列】在spring-data-Redis中如何使用切换库
前言 Redis默认有16个库,默认连接的是index=0的那一个.这16个库直接是相互独立的. 一.在命令行中切换 select 1; 二.在Spring中如何切换 1.在RedisConnecti ...
- CSS3 -- 动画库
http://www.jq22.com/yanshi819 文件结构: html <!DOCTYPE html> <html lang="zh-CN"> & ...
- Microsoft Security Essentials
https://support.microsoft.com/zh-cn/help/18900/consumer-antivirus-software-providers-for-windows 适 ...
- 题目1456:胜利大逃亡(广度优先搜索BFS)
题目链接:http://ac.jobdu.com/problem.php?pid=1456 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- 2-2 vue环境搭建以及vue-cli使用
一.vue多页面应用文件引用 1.官网拷贝: <script src="https://cdn.jsdelivr.net/npm/vue"></script> ...
- NHibernate.3.0.Cookbook第一章第六节Handling versioning and concurrency的翻译
NHibernate.3.0.Cookbook第一章第六节Handling versioning and concurrency的翻译 第一章第二节Mapping a class with XML ...
- phantomjs试玩
简单来说,phantomjs就是一个运行在node上的webkit内核,支持DOM渲染,css选择器,Canvas,SVG等,在浏览器上能做的事情,理论上,phantomjs 都能模拟做到. phan ...
- wordpress---page页面数据调用
在wordpress的开发中,会使用wordpress的的页面,那么页面数据该怎么调用呢? 拿到页面的 content: <?php if(have_posts()) : ?> <? ...