urllib库:解析链接
1from urllib.parse import urlparse, urlunparse, urlsplit, urlunsplit, urljoin, urlencode, parse_qs, parse_qsl, quote,unquote
1.urlparse() 解析url
1result = urlparse('https://mbd.baidu.com/newspage/data/landingsuper?context=%7B%22nid%22%3A%22news_9094304700265862330%22%7D&n_type=0&p_from=1')
2print(type(result), result)
3print(result.scheme)
4print(result[0])
2.urlunparse() 构造url 长度最长为6
1data = ['https', 'www.baidu.com', 'index.html', 'user', 'a=6', 'comment']
2print(urlunparse(data))
3.urlsplit() 把参数合并到path
1result = urlsplit('https://www.baidu.com/index.html;user?a=6#comment')
2print(result)
3print(result.scheme)
4print(result[0])
4.urlunsplit() 组成完整的链接
1data = ['https', 'www.baidu.com', 'index.html', 'a=6', 'comment']
2print(urlunsplit(data))
5.urljoin() 可以实现链接的解析,拼合,与生成
1print(urljoin("https://www.baidu.com/", 'FAQ.html'))
2print(urljoin('http://www.baiduu.com', 'https://www.gxj.com/FAQ.html'))
6.urlencode() 字典转化为请求参数
1params = {
2 'name': 'germey',
3 'age': 22
4}
5baseurl = 'http://www.baiduu.com?'
6url = baseurl + urlencode(params)
7print('--urlencode--%s' %url)
7.parse_qs() 转化为字典
1query = 'name=germey&age=22'
2print('--parse_qs---%s' %parse_qs(query))
8.parse_qsl() 参数转化为元祖组成的列表
1query = 'name=germey&age=22'
2print('--parse_qsl--%s' %parse_qsl(query))
9.quote() 将中文转化为英文
1keyword = '我爱你'
2url = 'http://www.baiduu.com' + quote(keyword)
3print('--quote()--%s'%url)
10.unquote() 将英文转化为中文
1keyword ='%E6%88%91%E7%88%B1%E4%BD%A0'
2url = 'http://www.baiduu.com' + unquote(keyword)
3print('--quote()--%s'%url)
urllib库:解析链接的更多相关文章
- urllib.parse解析链接
1. urlparse() 解析链接,注意,返回值比3多一个params的属性 from urllib.parse import urlparse result = urlparse('http:// ...
- python--爬虫入门(七)urllib库初体验以及中文编码问题的探讨
python系列均基于python3.4环境 ---------@_@? --------------------------------------------------------------- ...
- python爬虫 - Urllib库及cookie的使用
http://blog.csdn.net/pipisorry/article/details/47905781 lz提示一点,python3中urllib包括了py2中的urllib+urllib2. ...
- (爬虫)urllib库
一.爬虫简介 什么是爬虫?通俗来讲爬虫就是爬取网页数据的程序. 要了解爬虫,还需要了解HTTP协议和HTTPS协议:HTTP协议是超文本传输协议,是一种发布和接收HTML页面的传输协议:HTTPS协议 ...
- 爬虫之urllib库
一.urllib库简介 简介 Urllib是Python内置的HTTP请求库.其主要作用就是可以通过代码模拟浏览器发送请求.它包含四个模块: urllib.request :请求模块 urllib.e ...
- 一起学爬虫——urllib库常用方法用法总结
1.读取cookies import http.cookiejar as cj,urllib.request as request cookie = cj.CookieJar() handler = ...
- 爬取w3c课程—Urllib库使用
爬虫原理 浏览器获取网页内容的步骤:浏览器提交请求.下载网页代码.解析成页面,爬虫要做的就是: 模拟浏览器发送请求:通过HTTP库向目标站点发起请求Request,请求可以包含额外的header等信息 ...
- Python爬虫入门(3-4):Urllib库的高级用法
1.分分钟扒一个网页下来 怎样扒网页呢?其实就是根据URL来获取它的网页信息,虽然我们在浏览器中看到的是一幅幅优美的画面,但是其实是由浏览器解释才呈现出来的,实质它 是一段HTML代码,加 JS.CS ...
- 爬虫入门之urllib库详解(二)
爬虫入门之urllib库详解(二) 1 urllib模块 urllib模块是一个运用于URL的包 urllib.request用于访问和读取URLS urllib.error包括了所有urllib.r ...
随机推荐
- 5.反生成url
# url(r"^all/(?P<article_type_id>\d+)$", home.index,name="index" ), # 在htm ...
- Nowcoder Typing practice ( Trie 图 )
题目链接 题意 : 给出 n 个串.然后给出一个问询串.问你对于问询串的每一个前缀.需要至少补充多少单词才能使得其后缀包含 n 个串中的其中一个.注意 '-' 字符代表退格 分析 : 多串的匹配问询自 ...
- CF 25 E 三个字符串 KMP模板
Test Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Status ...
- hdu 2553 八皇后问题 基础
题意:给你一个n*n的棋盘,要求放n个皇后: <span style="font-size:18px;">#include <iostream> #incl ...
- Kaplan–Meier estimator & Greenwood formula
The Kaplan–Meier estimator (also known as the product limit estimator) estimates the survival functi ...
- [The Annotated Transformer] Iterators
Iterators 对torchtext的batch实现的修改算法原理 Batching matters a ton for speed. We want to have very evenly di ...
- 使用 vuetron 调试 mpvue 项目
简介 由于小程序开发工具的封闭,我们无法通过安装 chrome 插件来方便地使用 vue-devtools 调试我们的 mpvue 项目.vuetron 是一个 vue.js 的项目调试工具, 同时支 ...
- 套接字之select系统调用
select是IO多路复用的一种方式,用来等待一个列表中的多个描述符的可读可写状态: SYSCALL_DEFINE5(select, int, n, fd_set __user *, inp, fd_ ...
- shell script operate the date
How to increment a date in a bash script Use the date command's ability to add days to existing date ...
- pom文件中的dependencyManagement和dependencies的区别
dependencies 子项目中,自动继承父项目中的相关依赖 dependencyManagement 只是声明依赖,并不实现引入,因此子项目中需要显示的声明需要用的依赖.如果不在子项目中声明依赖, ...