爬虫--Urllib库详解
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库详解的更多相关文章
- 爬虫入门之urllib库详解(二)
爬虫入门之urllib库详解(二) 1 urllib模块 urllib模块是一个运用于URL的包 urllib.request用于访问和读取URLS urllib.error包括了所有urllib.r ...
- Python爬虫系列-Urllib库详解
Urllib库详解 Python内置的Http请求库: * urllib.request 请求模块 * urllib.error 异常处理模块 * urllib.parse url解析模块 * url ...
- python爬虫知识点总结(三)urllib库详解
一.什么是Urllib? 官方学习文档:https://docs.python.org/3/library/urllib.html 廖雪峰的网站:https://www.liaoxuefeng.com ...
- 爬虫(二):Urllib库详解
什么是Urllib: python内置的HTTP请求库 urllib.request : 请求模块 urllib.error : 异常处理模块 urllib.parse: url解析模块 urllib ...
- urllib库详解 --Python3
相关:urllib是python内置的http请求库,本文介绍urllib三个模块:请求模块urllib.request.异常处理模块urllib.error.url解析模块urllib.parse. ...
- Lua的协程和协程库详解
我们首先介绍一下什么是协程.然后详细介绍一下coroutine库,然后介绍一下协程的简单用法,最后介绍一下协程的复杂用法. 一.协程是什么? (1)线程 首先复习一下多线程.我们都知道线程——Thre ...
- Python--urllib3库详解1
Python--urllib3库详解1 Urllib3是一个功能强大,条理清晰,用于HTTP客户端的Python库,许多Python的原生系统已经开始使用urllib3.Urllib3提供了很多pyt ...
- Struts标签库详解【3】
struts2标签库详解 要在jsp中使用Struts2的标志,先要指明标志的引入.通过jsp的代码的顶部加入以下的代码: <%@taglib prefix="s" uri= ...
- STM32固件库详解
STM32固件库详解 emouse原创文章,转载请注明出处http://www.cnblogs.com/emouse/ 应部分网友要求,最新加入固件库以及开发环境使用入门视频教程,同时提供例程模板 ...
随机推荐
- 微信小程序wx.pageScrollTo的替代方案
wx.pageScrollTo这个微信小程序的api功能如下: 简而言之就是实现页面滚动的.但是在实际应用当中显得有些鸡肋,为啥呢?使用中有明显页面有明显的抖动,这无疑是极不好的用户体验.我用的华为6 ...
- windows批处理学习---01
一. 标记符号: CR(0D) 命令行结束符 Escape(1B) ANSI转义字符引导符 Space() 常用的参数界定符 Tab() ; = 不常用的参数界定符 + COPY命令文件连接符 * ? ...
- GetTickCount 和getTickCount
GetTickCount:正常读取时间函数 getTickCount:不知道是什么鬼东东函数 都包含在windows.h中..运行出的结果天壤之别~~~
- MySQL中的条件语句
判断学生表中成绩是否小于60,将小于60的学生成绩列为不及格 学生表(student) 字段:姓名(name),学号(主键)(num),性别(sex),成绩(score) select *,if(sc ...
- 第二章 IoC
什么是IoC 如何配置IOC Bean的生命周期 多环境配置 条件化配置Bean 什么是IOC? IOC有两层含义, 1.控制反转:将对象实例的创建与销毁的权限交给Spring容器管理,而不再是调用对 ...
- P4316 绿豆蛙的归宿
题意翻译 「Poetize3」 题目背景 随着新版百度空间的上线,Blog宠物绿豆蛙完成了它的使命,去寻找它新的归宿. 题目描述 给出一个有向无环图,起点为1终点为N,每条边都有一个长度,并且从起点出 ...
- python深浅copy-转自EVA的博客
感谢Eva_J, http://www.cnblogs.com/Eva-J/p/5534037.html,新手上路,转载纯为自己学习. 初学编程的小伙伴都会对于深浅拷贝的用法有些疑问,今天我们就结合p ...
- BZOJ5011 & 洛谷4065 & LOJ2275:[JXOI2017]颜色——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=5011 https://www.luogu.org/problemnew/show/P4065 ht ...
- BZOJ3571 & 洛谷3236:[HNOI2014]画框——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=3571 https://www.luogu.org/problemnew/show/P3236 小T ...
- BZOJ1293:[SCOI2009]生日礼物——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1293 https://www.luogu.org/problemnew/show/P2564#sub ...