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. iOS开发解决 jsonModel 属性跟系统的重复

    -(id)initWithDic:(NSDictionary *)dic { if (self = [super init]) { [self setValuesForKeysWithDictiona ...

  2. TCP系列30—窗口管理&流控—4、Cork算法

    一.Cork算法概述 Cork算法与Nagle算法类似,也有人把Cork算法称呼为super-Nagle.Nagle算法提出的背景是网络因为大量小包小包而导致利用率低下产生网络拥塞,网络发生拥塞的时候 ...

  3. mysqlslap工具测试mysql DB的性能

    mysqlslap的一个主要工作场景就是对数据库服务器做基准测试.     测试方法 1.测试工具:mysqlslap,mysqlslap是MySQL5.1.4之后自带的benchmark基准测试工具 ...

  4. MVC绕过登陆界面验证时HttpContext.Current.User.Identity.Name取值为空问题解决方法

    Global.asax界面添加如下方法: void FormsAuthentication_Authenticate(object sender, FormsAuthenticationEventAr ...

  5. java 中使用Base64

    byte[] cipherData = Base64.encodeBase64(plainText.getBytes()); //默认不换行 byte[] cipherData = Base64.en ...

  6. 【bzoj1727】[Usaco2006 Open]The Milk Queue 挤奶队列 贪心

    题目描述 Every morning, Farmer John's N (1 <= N <= 25,000) cows all line up for milking. In an eff ...

  7. AtCoder Regular Contest 103 题解

    C-/\/\/\ #include<algorithm> #include<iostream> #include<cstdlib> #include<ioma ...

  8. [BZOJ4822] [CQOI2017] 老C的任务

    题目链接 BZOJ:https://lydsy.com/JudgeOnline/problem.php?id=4822. 洛谷:https://www.luogu.org/problemnew/sho ...

  9. Random Numbers Gym - 101466K dfs序+线段树

    Tamref love random numbers, but he hates recurrent relations, Tamref thinks that mainstream random g ...

  10. udhcpd源码分析3--IP租赁管理

    1:重要的结构体 全局链表的成员struct dhcpOfferedAddr *leases 记录了当前租赁出去的IP信息 /* leases.h */ struct dhcpOfferedAddr ...