
2.1 urllib库的使用(非重点)
urllib的官方文档
urllib是Python中自带的http请求库,也就是说不用额外安装就可以使用,它包含如下四个模块:
requests:基本的http请求模块,可以模拟发送请求error:异常处理模块parse:一个工具模块,提供了许多URL处理方法,比如拆分、解析、合并等。robotparser:它主要用来识别网站的robots.txt文件,让后判断哪些内容可以爬取,哪些不能爬取,用得比较少。2.1.1 request模块
发送请求
# 2.1 使用urllib库中的request模块发送一个请求的例子import urllib.requestresponse = urllib.request.urlopen('http://www.baIDu.com')print(response.read().decode('utf-8'))使用request.urlopen()来向百度首页发起请求,返回的是http.clIEnt.httpResponse对象,这个对象主要包含read()、readinto()、getheader(name)、getheaders()、fileno()等方法,以及msg、version、status、reason、deBUGlevel、closed等属性。将返回的HTML代码以utf-8的编码方式读取并打印出来。上面的代码执行后将返回百度的主页的HTML代码。
我运行的效果如下:
<!DOCTYPE HTML><!--STATUS OK--> <HTML><head><Meta http-equiv="Content-Type" content="text/HTML;charset=utf-8"><Meta http-equiv="X-UA-Compatible" content="IE=edge,Chrome=1"><Meta content="always" name="referrer"><Meta name="theme-color" content="#2932e1"><Meta name="description" content=" 概述第二章基本库的使用2.1urllib库的使用(非重点)urllib的官方文档urllib是Python中自带的HTTP请求库,也就是说不用额外安装就可以使用,它包含如下四个模块:requests:">后面省略无数字......接下来再看这个例子:
# 2.2 使用urllib中的request模块发起一个请求并获取response对象中的信息的例子import urllib.requestresponse = urllib.request.urlopen("http://www.python.org")print(response.read().decode('utf-8')[:100]) # 截取返回的HTML代码的前100个字符的信息print("response的类型为:" + str(type(response)))print("response的状态为:" + str(response.status))print("response的响应的头信息:" + str(response.getheaders()))print("response的响应头中的Server值为:" + str(response.getheader('Server')))上面的代码使用urlopen()方法向指定的链接发起了一个请求,得到一个httpResponse对象,然后调用httpResponse的方法和属性来获取请求的状态、请求头信息等
下面是我的执行结果:
<!DOCTYPE HTML><!--[if lt IE 7]> <HTML > <![endif]--><!-response的类型为:<class 'http.clIEnt.httpResponse'>response的状态为:200response的响应的头信息:[('Connection', 'close'), ('Content-Length', '50890'), ('Server', 'Nginx'), ('Content-Type', 'text/HTML; charset=utf-8'), ('x-frame-options', 'DENY'), ('Via', '1.1 vegur, 1.1 varnish, 1.1 varnish'), ('Accept-Ranges', 'bytes'), ('Date', 'Mon, 17 May 2021 08:59:57 GMT'), ('Age', '1660'), ('X-Served-By', 'cache-bwi5163-BWI, cache-hkg17920-HKG'), ('X-Cache', 'HIT, HIT'), ('X-Cache-Hits', '1, 3886'), ('X-Timer', 'S1621241997.260514,VS0,VE0'), ('vary', 'cookie'), ('Strict-Transport-Security', 'max-age=63072000; includeSubDomains')]response的响应头中的Server值为:Nginxdata参数
data参数是可选的,该参数是bytes类型,需要使用bytes()方法将字典转化为字节类型,并且,该参数只能在POST请求中使用。
# 2.3 data参数的使用import urllib.request# 使用urllib中的parse模块中的urlencode方法来将字典转化为字节类型,编码方式为utf-8data = bytes(urllib.parse.urlencode({'word': 'hello'}), enCoding='utf8')response = urllib.request.urlopen('http://httpbin.org/post', data=data)print(response.read())这次我们请求的是http://httpbin.org/post这个网址,这个网址可以提供http请求测试,它可以返回请求的一些信息,其中包括我们传递的data参数。
timeout参数
timeout参数用来设置超时时间,单位为秒,意思是如果请求超出了设置的这个时间,还没有得到响应,就会抛出异常。
# 2.4 timeout参数的使用import urllib.requestresponse = urllib.request.urlopen('http://www.baIDu.com', timeout=1)print(response.read())运行结果就不展示了。
其他参数
除了data参数和timeout参数外,还有context参数,它必须是ssl.SSLContext类型,用来指定SSL设置
Request类
urlopen()可以实现基本的请求的发起,但这不能构造一个完整的请求,如果要在请求中加入headers等信息,就可以利用更强大的Request类来构建。
# 2.5 Request类的使用import urllib.requestrequest = urllib.request.Request('https://python.org')print(type(request))response = urllib.request.urlopen(request) # 传入的是Request对象print(response.read().decode('utf-8'))request的构造方法:
Requests(url, data, headers, origin_host, unverifiablem, method)
url:请求的url链接data:必须为字节流(bytes)类型headers:请求头信息origin_req_host:请求方的host名称或者IP地址unverifiable:表示这个请求是否是无法验证的,默认为False。method:指示请求使用的方法,比如:GET、POST、PUT等
下面是例子:
# 2.6 Request类的使用from urllib import request, parseurl = "http://httpbin.org/get"headers = { 'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; windows NT)', 'Host': 'httpbin.org'}dict = { 'name': 'Germey'}data = bytes(parse.urlencode(dict), enCoding='utf8')req = request.Request(url=url, data=data, headers=headers, method='GET')response = request.urlopen(req)print(response.read().decode('utf-8'))我们依然请求的是测试网址http://httpbin.org/get,它会返回我们发起的请求信息,下面是我的运行结果:
{ "args": {}, "headers": { "Accept-EnCoding": "IDentity", "Content-Length": "11", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "Mozilla/4.0 (compatible; MSIE 5.5; windows NT)", "X-Amzn-Trace-ID": "Root=1-60a236ed-01f68c862b09c8934983ae80" }, "origin": "221.176.140.213", "url": "http://httpbin.org/get"}从结果中,我们可以看到我们发起的请求中包含了我们自己设置的User-Agent,Host和我们请求中包含的数据 ‘name’: ‘Germey’。
2.1.2 error模块
urllib中的error模块定义了由request模块产生的异常,如果出现了问题,request模块就会抛出error模块中的异常。
下面介绍其中用得比较多的两个异常:URLError和httpError。
URLError
URLError类是error异常模块的基类,由request模块产生的异常都可以通过捕获这个异常来处理。
# 2.7 URLError的使用例子from urllib import request, error# 打开一个不存在的网页try: response = request.urlopen('https://casdfasf.com/index.htm')except error.URLError as e: print(e.reason) 运行结果:
[Errno 11001] getaddrinfo Failed httpError
它是URLError的子类,专门用来处理http请求错误,比如认证请求失败等。它有如下3个属性:
code:返回http状态码reason:返回错误的原因headers:返回请求头# 2.8 httpError对象的属性from urllib import request, errortry: response = request.urlopen('https://cuiqingcai.com/index.htm')except error.httpError as e: print(e.reason, e.code, e.headers, sep='\n') 运行结果:
Not Found404Server: GitHub.comDate: Tue, 16 Feb 2021 03:01:45 GMTContent-Type: text/HTML; charset=utf-8X-NWS-UUID-VERIFY: 8e28a376520626e0b40a8367b1c3ef01Access-Control-Allow-Origin: *ETag: "6026a4f6-c62c"x-proxy-cache: MISSX-GitHub-Request-ID: 0D4A:288A:10EE94:125FAD:602B33C2Accept-Ranges: bytesAge: 471Via: 1.1 varnishX-Served-By: cache-tyo11941-TYOX-Cache: HITX-Cache-Hits: 0X-Timer: S1613444506.169026,VS0,VE0vary: Accept-EnCodingX-Fastly-Request-ID: 9799b7e3df8bdc203561b19afc32bb5803c1f03cX-Daa-Tunnel: hop_count=2X-Cache-Lookup: Hit From UpstreamX-Cache-Lookup: Hit From Inner ClusterContent-Length: 50732X-NWS-LOG-UUID: 5426589989384885430Connection: closeX-Cache-Lookup: Cache Miss 2.1.3 parse模块
parse模块是用来处理url的模块,它可以实现对url各部分的抽取、合并以及连接装换等。
下面介绍parse模块中常用的几个方法:
urlparse()
实现url的识别和分段
# 2.9 urllib库中parse模块中urlparse()方法的使用from urllib.parse import urlparseresult = urlparse('http://www.biadu.com/index.HTML;user?ID=5#comment')print(type(result), result)result1 = urlparse('www.biadu.com/index.HTML;user?ID=5#comment', scheme='https')print(type(result1), result1)result2 = urlparse('http://www.baIDu.com/index.HTML#comment', allow_fragments=False)print(type(result2), result2)result3 = urlparse('http://www.baIDu.com/index.HTML#comment', allow_fragments=False)print(result3.scheme, result3[0], result3.netloc, result3[1], sep="\n") 运行结果:
<class 'urllib.parse.ParseResult'> ParseResult(scheme='http', netloc='www.biadu.com', path='/index.HTML', params='user', query='ID=5', fragment='comment')<class 'urllib.parse.ParseResult'> ParseResult(scheme='https', netloc='', path='www.biadu.com/index.HTML', params='user', query='ID=5', fragment='comment')<class 'urllib.parse.ParseResult'> ParseResult(scheme='http', netloc='www.baIDu.com', path='/index.HTML#comment', params='', query='', fragment='')httphttpwww.baIDu.comwww.baIDu.com 可以看到,urlparse()方法将url解析为6部分,返回的是一个ParseResult对象,这6部分是:
scheme: 总结以上是内存溢出为你收集整理的Python爬虫基本库的使用全部内容,希望文章能够帮你解决Python爬虫基本库的使用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)