1、什么是Urllib?

2、相比Python2的变化

3、用法讲解

(1)urlopen

urlllb.request.urlopen(url,data=None[timeout,],cahle=None,capath=None,cadefault=False,context=None)
#第一个参数为url网址,第二个参数为额外的数据,第三个参数为超时的设置,剩下的参数暂时用不到
######### GET 类型的请求 #############
import urllib.request
response =urllib.request.urlopen("http://ww.baidu.com")
print(response.read().decode("utf-8")
<!DOCTYPE html>
<!--STATUS OK--> ······················· ······················ ····················· <script>
if(navigator.cookieEnabled){
document.cookie="NOJS=;expires=Sat, 01 Jan 2000 00:00:00 GMT";
}
</script> </body>
</html>

打印的结果为:

######### POST 类型的请求 #############
import urllib.request
import urllib.parse data=bytes(urllib.parse.urlencode({'word':'hello'}),encoding='utf8')
response=urllib.request.urlopen("http://httpbin.org/post",data=data) # http://httpbin.org/post HTTP测试的网址
print(response.read())
b'{\n  "args": {}, \n  "data": "", \n  "files": {}, \n  "form": {\n    "word": "hello"\n  }, \n  "headers": {\n    "Accept-Encoding": "identity", \n    "Connection": "close", \n    "Content-Length": "10", \n    "Content-Type": "application/x-www-form-urlencoded", \n    "Host": "httpbin.org", \n    "User-Agent": "Python-urllib/3.5"\n  }, \n  "json": null, \n  "origin": "221.208.253.76", \n  "url": "http://httpbin.org/post"\n}\n'

打印的结果为:

import urllib.request
############### 超时的设置 ###############
response=urllib.request.urlopen("http://httpbin.org/get",timeout=1) # 设置一个超时的时间,在规定的时间没有响应,则抛出异常
print(response.read())
b'{\n "args": {}, \n "headers": {\n "Accept-Encoding": "identity", \n "Connection": "close", \n "Host": "httpbin.org", \n "User-Agent": "Python-urllib/3.5"\n }, \n "origin": "221.208.253.76", \n "url": "http://httpbin.org/get"\n}\n'

打印的结果为:

import urllib.request
import urllib.error
import socket ############### 超时的设置,超出响应时间 ############### try:
response = urllib.request.urlopen('htp://httpbin.org/get', timeout=0.1)
except urllib.error.URLError as e:
if isinstance(e.reason,socket.timeout):
print("Time out")
 Time out

打印的结果为:

 (2)响应

响应类型

import urllib.request
response=urllib.request.urlopen('https://www.python.org')
print(type(response))
<class 'http.client.HTTPResponse'>

打印的结果为:

状态码、响应头

import urllib.request
response =urllib.request.urlopen('https://www.python.org')
print(response.status) # 获取状态码
print(response.getheaders) # 获取响应头
print(response.getheader('Server'))
200
<bound method HTTPResponse.getheaders of <http.client.HTTPResponse object at 0x0000000002D04EB8>>
nginx

打印的结果为:

(3)request

import urllib.request
request=urllib.request.Request("https://python.org")
response=urllib.request.urlopen(request)
print(response.read().decode("utf-8"))
<!doctype html>
<!--[if lt IE 7]> <html class="no-js ie6 lt-ie7 lt-ie8 lt-ie9"> <![endif]-->
<!--[if IE 7]> <html class="no-js ie7 lt-ie8 lt-ie9"> <![endif]-->
<!--[if IE 8]> <html class="no-js ie8 lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--><html class="no-js" lang="en" dir="ltr"> <!--<![endif]--> <head> ················· ···················· </body>
</html>

打印的结果为:

from urllib import request,parse
url='http://httpbin.org/post' ############ POST 请求 ###############
headers={
"User-Agent":"Mozilla/4.0(compatible;MSIE 5.5;Windows NT)",
"Host":'httpbin.org'
}
dict={
'name':"Germey"
}
data =bytes(parse.urlencode(dict),encoding="utf-8")
req =request.Request(url=url,data=data,headers=headers,method='POST')
response=request.urlopen(req)
print(response.read().decode('utf-8'))
{
"args": {},
"data": "",
"files": {},
"form": {
"name": "Germey"
},
"headers": {
"Accept-Encoding": "identity",
"Connection": "close",
"Content-Length": "",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "Mozilla/4.0(compatible;MSIE 5.5;Windows NT)"
},
"json": null,
"origin": "221.208.253.76",
"url": "http://httpbin.org/post"
}

打印的结果为:


from urllib import request,parse
url ="http://httpbin.org/post"
dict={
'name':'Germey'
}
data =bytes(parse.urlencode(dict),encoding='utf8')
req = request.Request(url=url,data=data,method="POST")
req.add_header('User-Agent','Mozilla/4.0(compatible;MSIE5.5;Windows NT)')
response = request.urlopen(req)
print(response.read().decode('utf-8'))
{
"args": {},
"data": "",
"files": {},
"form": {
"name": "Germey"
},
"headers": {
"Accept-Encoding": "identity",
"Connection": "close",
"Content-Length": "",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "Mozilla/4.0(compatible;MSIE5.5;Windows NT)"
},
"json": null,
"origin": "221.208.253.76",
"url": "http://httpbin.org/post"
}

打印的结果为:

(4)Handler

代理

import urllib.request
proxy_handler = urllib.request.ProxyHandler({
'http':'http://127.0.0.1:9743', # 代理http
'https':'https://127.0.0.1:9743' # 代理https
})
opener =urllib.request.build_opener(proxy_handler)
response=opener.open("http://www.baidu.com")
print(response.read())
因为我没有代理,所以打印出来的结果为:

urllib.error.URLError: <urlopen error [WinError 10061] 由于目标计算机积极拒绝,无法连接。>

打印的结果为:

Cookie

import http.cookiejar,urllib.request
cookie=http.cookiejar.CookieJar() # 获取Cookie信息
handler=urllib.request.HTTPCookieProcessor(cookie) # 把Cookie信息放入到 handler中
opener=urllib.request.build_opener(handler) # 建立opener
response=opener.open("http://www.baidu.com")
for item in cookie:
print(item.name+"=”+item.value)
BAIDUID=DDCB4C216AE8EE90C7D95E7AF8FA577F:FG=1
BIDUPSID=DDCB4C216AE8EE90C7D95E7AF8FA577F
H_PS_PSSID=1452_21078_26350_27111
PSTM=1536830732
BDSVRTM=0
BD_HOME=0
delPer=0

打印的结果为:

########### 把Cookie 保存成文件 ##########
import http.cookiejar,urllib.request
filename = "cookie.txt"
cookie=http.cookiejar.MozillaCookieJar(filename)
handler=urllib.request.HTTPCookieProcessor(cookie)
opener=urllib.request.build_opener(handler)
response=opener.open("http://www.baidu.com")
cookie.save(ignore_discard=True,ignore_expires=True)
在工程目录下多了一个cookie.txt文件

该文件的内容为:

# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This is a generated file! Do not edit. .baidu.com TRUE / FALSE 3684314677 BAIDUID CB67C520D33E28D7204C570EB7DFA28F:FG=1
.baidu.com TRUE / FALSE 3684314677 BIDUPSID CB67C520D33E28D7204C570EB7DFA28F
.baidu.com TRUE / FALSE H_PS_PSSID 1434_21113_26350_20930
.baidu.com TRUE / FALSE 3684314677 PSTM 1536831034
www.baidu.com FALSE / FALSE BDSVRTM 0
www.baidu.com FALSE / FALSE BD_HOME 0
www.baidu.com FALSE / FALSE 2482910974 delPer 0

打印的结果为:

########### 另一种 Cookie 的保存案例 ##########
import http.cookiejar,urllib.request
filename = "cookies.txt"
cookie=http.cookiejar.LWPCookieJar(filename)
handler=urllib.request.HTTPCookieProcessor(cookie)
opener=urllib.request.build_opener(handler)
response=opener.open("http://www.baidu.com")
cookie.save(ignore_discard=True,ignore_expires=True)

代码运行结果与上面相同!

(5)异常处理

from urllib import request,error
try:
response=request.urlopen("http://cuiqingcai.com/index.htm")
except error.URLError as e:
print(e.reason)
Not Found

打印的结果为:

from urllib import request,error
try:
response =request.urlopen('http://cuiqingcai.com/index.htm')
except error.HTTPError as e:
print(e.reason,e.code,e.headers,sep='\n')
except error.URLError as e:
print(e.reason)
else:
print("Request Successfully")
Not Found
404
Server: nginx/1.10.3 (Ubuntu)
Date: Thu, 13 Sep 2018 11:08:18 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: close
Vary: Cookie
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
Link: <https://cuiqingcai.com/wp-json/>; rel="https://api.w.org/"

打印的结果为:

import socket
import urllib.request
import urllib.error
try:
response = urllib.request.urlopen("https://www.baidu.com",timeout=0.000000001)
except urllib.error.URLError as e:
print(type(e.reason))
if isinstance(e.reason,socket.timeout):
print("TimeOut")
<class 'socket.timeout'>
TimeOut

执行后的结果为:

 (6)URL解析

urlparse

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

打印的结果为:

######## 无协议类型 ###########
from urllib.parse import urlparse
result =urlparse("www.baidu.com/index.html;user?id=5i#comment,scheme=/https")
print(result)
ParseResult(scheme='', netloc='', path='www.baidu.com/index.html', params='user', query='id=5i', fragment='comment,scheme=/https')

打印后的结果为:

######## 默认的协议类型 ###########
from urllib.parse import urlparse
result=urlparse("http://www.baidu.com/index.html;user?id=5i#comment,scheme=/https")
print(result)
ParseResult(scheme='http', netloc='www.baidu.com', path='/index.html', params='user', query='id=5i', fragment='comment,scheme=/https')

打印后的结果为:

from urllib.parse import urlparse
result =urlparse("http://www.baidu.com/index.html;user?id=5i#comment",allow_fragments=False)
print(result)
ParseResult(scheme='http', netloc='www.baidu.com', path='/index.html', params='user', query='id=5i#comment', fragment='')

打印后的结果为:

from urllib.parse import urlparse
result =urlparse("http://www.baidu.com/index.htmlf#comment",allow_fragments=False)
print(result)
ParseResult(scheme='http', netloc='www.baidu.com', path='/index.htmlf#comment', params='', query='', fragment='')

打印后的结果为:

urlunparse

from urllib.parse import urlunparse
data =["http","www.baidu.cogn","index.html","user",'a=6','comment']
print(urlunparse(data))
http://www.baidu.cogn/index.html;user?a=6#comment

执行后的结果

 urljoin(url拼接,前面若在为补充,后面若在为基准)

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/infex.php'))
print(urljoin('http://www.baidu.com','?category=2#commen:'))
print(urljoin('www.baidu.com','?category=2t#comment'))
print(urljoin('www.baidu.comi#comment','?category=2'))
http://www.baidu.com/FAQ.htmr
https://cuiqingcai.com/FAQ.html
https://cuiqingcai.com/FAQ.html
https://cuiqingcai.com/FAQ.html?question=2
https://cuiqingcai.com/infex.php
http://www.baidu.com?category=2#commen:
www.baidu.com?category=2t#comment
www.baidu.comi?category=2

打印的结果为:

urlencode(把字典对象转化为GET请求参数)

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

打印的结果为:

爬虫--Urllib库详解的更多相关文章

  1. 爬虫入门之urllib库详解(二)

    爬虫入门之urllib库详解(二) 1 urllib模块 urllib模块是一个运用于URL的包 urllib.request用于访问和读取URLS urllib.error包括了所有urllib.r ...

  2. Python爬虫系列-Urllib库详解

    Urllib库详解 Python内置的Http请求库: * urllib.request 请求模块 * urllib.error 异常处理模块 * urllib.parse url解析模块 * url ...

  3. python爬虫知识点总结(三)urllib库详解

    一.什么是Urllib? 官方学习文档:https://docs.python.org/3/library/urllib.html 廖雪峰的网站:https://www.liaoxuefeng.com ...

  4. 爬虫(二):Urllib库详解

    什么是Urllib: python内置的HTTP请求库 urllib.request : 请求模块 urllib.error : 异常处理模块 urllib.parse: url解析模块 urllib ...

  5. urllib库详解 --Python3

    相关:urllib是python内置的http请求库,本文介绍urllib三个模块:请求模块urllib.request.异常处理模块urllib.error.url解析模块urllib.parse. ...

  6. Lua的协程和协程库详解

    我们首先介绍一下什么是协程.然后详细介绍一下coroutine库,然后介绍一下协程的简单用法,最后介绍一下协程的复杂用法. 一.协程是什么? (1)线程 首先复习一下多线程.我们都知道线程——Thre ...

  7. Python--urllib3库详解1

    Python--urllib3库详解1 Urllib3是一个功能强大,条理清晰,用于HTTP客户端的Python库,许多Python的原生系统已经开始使用urllib3.Urllib3提供了很多pyt ...

  8. Struts标签库详解【3】

    struts2标签库详解 要在jsp中使用Struts2的标志,先要指明标志的引入.通过jsp的代码的顶部加入以下的代码: <%@taglib prefix="s" uri= ...

  9. STM32固件库详解

    STM32固件库详解   emouse原创文章,转载请注明出处http://www.cnblogs.com/emouse/ 应部分网友要求,最新加入固件库以及开发环境使用入门视频教程,同时提供例程模板 ...

随机推荐

  1. PHP连接Redis操作函数

    phpredis是php的一个扩展,效率是相当高有链表排序功能,对创建内存级的模块业务关系 很有用;以下是redis官方提供的命令使用技巧: 下载地址如下: https://github.com/ow ...

  2. opencv里vector的内存的申请和释放的问题

    改成: ); CvSeq * m_contour=; cvFindContours( &IPlImage(img), m_storage, &m_contour, ,)); //释放内 ...

  3. mysql,oracle,sql server中的默认事务隔离级别查看,更改

    未提交读(隔离事务的最低级别,只能保证不读取物理上损坏的数据) 已提交读(数据库引擎的默认级别) 可重复读 可序列化(隔离事务的最高级别,事务之间完全隔离) 可串行化比较严谨,级别高; MySQL m ...

  4. K-means聚类算法与EM算法

    K-means聚类算法 K-means聚类算法也是聚类算法中最简单的一种了,但是里面包含的思想却不一般. 聚类属于无监督学习.在聚类问题中,给我们的训练样本是,每个,没有了y. K-means算法是将 ...

  5. BZOJ 1070 修车(费用流)

    如果能想到费用流,这道题就是显然了. 要求所有人的等待平均时间最小,也就是所有人的总等待时间最小. 每辆车只需要修一次,所以s连每辆车容量为1,费用为0的边. 现在需要把每个人拆成n个点,把车和每个人 ...

  6. 【bzoj4832】[Lydsy2017年4月月赛]抵制克苏恩 概率期望dp

    题目描述 你分别有a.b.c个血量为1.2.3的奴隶主,假设英雄血量无限,问:如果对面下出一个K点攻击力的克苏恩,你的英雄期望会受到到多少伤害. 输入 输入包含多局游戏. 第一行包含一个整数 T (T ...

  7. 【bzoj1614】[Usaco2007 Jan]Telephone Lines架设电话线 二分+SPFA

    题目描述 Farmer John打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司支付一定的费用. FJ的农场周围分布着N(1 <= N <= 1 ...

  8. 【bzoj1606】[Usaco2008 Dec]Hay For Sale 购买干草 背包dp

    题目描述 约翰遭受了重大的损失:蟑螂吃掉了他所有的干草,留下一群饥饿的牛.他乘着容量为C(1≤C≤50000)个单位的马车,去顿因家买一些干草.  顿因有H(1≤H≤5000)包干草,每一包都有它的体 ...

  9. 【刷题】BZOJ 3546 [ONTAK2010]Life of the Party

    Description 一个舞会有N个男孩(编号为1..N)和M个女孩(编号为1..M),一对男女能够组成一对跳舞当且仅当他们两个人互相认识. 我们把一种人定义成这个舞会的life:当且仅当如果他(她 ...

  10. BZOJ3675 [Apio2014]序列分割 【斜率优化dp】

    3675: [Apio2014]序列分割 Time Limit: 40 Sec  Memory Limit: 128 MB Submit: 3366  Solved: 1355 [Submit][St ...