什么是Urllib库?

 Urllib是Python提供的一个用于操作URL的模块。在Python2.X中,有Urllib库,也有Urllib2库,但是在最新的Python3.X中,将Urllib2合并到了Urllib中,这个库在我们爬取网页的时候会经常用到。

升级合并后,模块中的包的位置变化的地方较多,以下是一些常见的变动:

在Python2.X中使用import urllib2,在Python3.X中会用到import urllib.request,urllib.error

在Python2.X中使用import urllib,在Python3.X中会用到import urllib.request,urllib.error,urllib.parse

在Python2.X中使用import urlparse,在Python3.X中会用到import urllib.parse

在Python2.X中使用import urlopen,在Python3.X中会用到import urllib.request.urlopen

在Python2.X中使用import urlencode,在Python3.X中会用到import urllib.parse.urlencode

在Python2.X中使用import urllib.quote,在Python3.X中会用到import urllib.request.quote

在Python2.X中使用import cookielib.CookieJar,在Python3.X中会用到import http.CookeiJar

在Python2.X中使用import urllib2.Requset,在Python3.X中会用到import urllib.request.Request

 

其中,这里主要介绍Python3.X中各个模块的作用

Python3.6.0中urllib模块包括一下四个子模块:


urllib is a package that collects several modules for working with URLs:

urllib.request for opening and reading URLs
urllib.error containing the exceptions raised by urllib.request
urllib.parse for parsing URLs
urllib.robotparser for parsing robots.txt files
官方解释是如下:
urllib模块是一个运用于URL的包

urllib.request用于访问和读取URLS

urllib.error包括了所有urllib.request导致的异常

urllib.parse用于解析URLS

urllib.robotparser用于解析robots.txt文件(网络蜘蛛)
 

urllib.request.urlopen该函数主要用于打开一个URL网页:

urlopen一般常用的有三个参数,它的参数如下:
urllib.requeset.urlopen(url,data,timeout)
# -*- coding:utf-8 -*- 
#获取并打印google首页的html
import urllib.request
response=urllib.request.urlopen('http://www.google.com')
html=response.read()
print(html)
urlopen返回一个类文件对象,可以像文件一样操作,同时支持一下三个方法:

info():返回一个对象,表示远程服务器返回的头信息。
getcode():返回Http状态码,如果是http请求,200表示请求成功完成;404表示网址未找到。
geturl():返回请求的url地址
 

URL解析

urlparse
The URL parsing functions focus on splitting a URL string into its components, or on combining URL components into a URL string.

urllib.parse.urlparse(urlstring, scheme='', allow_fragments=True)

函数用于将一个URL解析成六个部分,返回一个元组,URL的格式为:scheme://netloc/path;parameters?query#fragment;包含六个部分,元组中每一个元素都是一个字符串,可以为空,这六个部分均不能再被分割成更小的部分;

以下为返回的元组元素:                             

元素      编号 值     值不存在时默认值
 scheme
 
0  请求一 定存在
netloc1网址 空字符串
path2分层路径空字符串
params3参数空字符串
query4查询组件空字符串
fragment5标识符 空字符串
username6用户名   None
password7密码 None
hostname8主机名    None
port9端口号 None

import urllib.parse
 
print(urllib.parse.urlparse("https://www.zhihu.com/question/50056807/answer/223566912"))
ParseResult(scheme='https', netloc='www.zhihu.com', path='/question/50056807/answer/223566912', params='', query='', fragment='')
 

 

   

 

  
 

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值