【python爬虫自学笔记】-----urllib库的基本使用

urllib是python内置的http请求库包含以下模块:

urllib.request 请求模块,用来打开和读取url;

urllib.error 异常处理模块 ,包含request产生的错误,可以使用try进行捕捉处理;

urllib.parse url解析模块,包含一些解析url的方法;

urllib.robotparser robots.txt解析模块,它提供了一个单独的RobotFileParser类,通过该类提供的can_fetch()方法测试爬虫是否可以下载一个页面;

使用urllib.request.urlopen()函数打开一个网站,读取并打印

from urllib import request
response = request.urlopen('http://www.baidu.com')
html = response.read()
print(html)

urlopen

urllib.request.urlopen(url,data=None,[timeout]*,cafile=None,capath=None,cadefault=False,context=None)

使用的三个只要参数为url,data,timeout,使用response.read()可以获取到网页的内容

data参数的使用

import urllib.parse
data = bytes(urllib.parse.urlencode({'word':'hello'}),encoding='utf8')
print(data)
response = urllib.request.urlopen('http://httpbin.org/post',data=data)
print(response.read())

b'word=hello'
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.6"\n  }, \n  "json": null, \n  "origin": "121.17.200.18", \n  "url": "http://httpbin.org/post"\n}\n'

通过 bytes(urllib.parse.urlencode())可以将post数据进行转换放到urllib.request.urlopen的data参数中,这样就完成了一次post请求。

如果添加data参数的时候就是post请求,如果没有data参数就是get请求。

timeout参数使用

在某些网络情况不好或者服务器端异常的情况下会出现请求慢的情况,或者请求异常,此时需要设置一个超时时间。

from urllib import request
response = request.urlopen('http://httpbin.org/get',timeout=1)
html = response.read()
print(html)

b'{\n  "args": {}, \n  "headers": {\n    "Accept-Encoding": "identity", \n    "Connection": "close", \n    "Host": "httpbin.org", \n    "User-Agent": "Python-urllib/3.6"\n  }, \n  "origin": "121.17.200.18", \n  "url": "http://httpbin.org/get"\n}\n'

将超时参数改为0.1

from urllib import request
response = request.urlopen('http://httpbin.org/get',timeout=0.1)
html = response.read()
print(html)

Traceback (most recent call last):
  File "D:\develop\Anaconda3\lib\urllib\request.py", line 1318, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "D:\develop\Anaconda3\lib\http\client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "D:\develop\Anaconda3\lib\http\client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "D:\develop\Anaconda3\lib\http\client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "D:\develop\Anaconda3\lib\http\client.py", line 1026, in _send_output
    self.send(msg)
  File "D:\develop\Anaconda3\lib\http\client.py", line 964, in send
    self.connect()
  File "D:\develop\Anaconda3\lib\http\client.py", line 936, in connect
    (self.host,self.port), self.timeout, self.source_address)
  File "D:\develop\Anaconda3\lib\socket.py", line 724, in create_connection
    raise err
  File "D:\develop\Anaconda3\lib\socket.py", line 713, in create_connection
    sock.connect(sa)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:/thislove/pythonworkspace/blogspark/baidu.py", line 13, in <module>
    response = urllib.request.urlopen('http://httpbin.org/get', timeout=0.1)
  File "D:\develop\Anaconda3\lib\urllib\request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "D:\develop\Anaconda3\lib\urllib\request.py", line 526, in open
    response = self._open(req, data)
  File "D:\develop\Anaconda3\lib\urllib\request.py", line 544, in _open
    '_open', req)
  File "D:\develop\Anaconda3\lib\urllib\request.py", line 504, in _call_chain
    result = func(*args)
  File "D:\develop\Anaconda3\lib\urllib\request.py", line 1346, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "D:\develop\Anaconda3\lib\urllib\request.py", line 1320, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error timed out>

出现异常timed out

对异常进行抓取

import  socket
import urllib.error
from urllib import request
try:
    response = request.urlopen('http://httpbin.org/get',timeout=0.1)
except urllib.error.URLError as e:
    if isinstance(e.reason,socket.timeout):
        print('TIME OUT')

TIME OUT

request

响应    响应类型、状态码、响应头

import urllib.request
response = urllib.request.urlopen('http://www.python.org')
print(type(response))

<class 'http.client.HTTPResponse'>

可以使用response.status、response.getheaders()、response.getheader('server')来获取状态码以及头部信息;使用response.read()获得响应体内容;

设置headers:许多网站为了防止程序爬虫导致网站瘫痪,会需要携带一些headers头部信息才能访问,常见的是user-agent参数;

from urllib import  request,parse
url = 'http://httpbin.org/post'
headers = {
    'User-Agent':'Mozilla/4.0(compatible;MSIE 5.5; Windows NT)',
    'Host':'httpbin.org'
}
dict = {
    'name':'zhaofan'
}
data = bytes(parse.urlencode(dict),encoding='utf8')
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": "zhaofan"
  }, 
  "headers": {
    "Accept-Encoding": "identity", 
    "Connection": "close", 
    "Content-Length": "12", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "Mozilla/4.0(compatible;MSIE 5.5; Windows NT)"
  }, 
  "json": null, 
  "origin": "121.17.200.18", 
  "url": "http://httpbin.org/post"
}

高级用法代理,ProxyHandler

通过urllib.request.ProxyHandler()可以设置代理,网站它会检测某一段时间某个IP的访问次数,如果访问次数过多会禁止访问,这个时候需要通过设置代理来爬取数据。

#设置代理
import urllib.request

proxy_handler = urllib.request.ProxyHandler({
    'http': 'http://127.0.0.1:9743',
    'https': 'https://127.0.0.1:9743'
})
opener = urllib.request.build_opener(proxy_handler)
response = opener.open('http://httpbin.org/get')
print(response.read())

HTTP的put和delete方法

http协议有六种请求方法:get,head,put,delete,post,options。

PUT:HTML表单不支持,本质上,PUT和POST极为相似,都是向服务器发送数据,他们之间的区别是:put通常指定资源的存放位置,而POST没有,POST的数据存放位置由服务器自己决定。

delete:删除某一个资源。

cookie,HTTPCookieProcessor

cookie(某些网站为了辨别用户的身份进行session跟踪而储存在用户本地终端上的数据(通常经过加密))中保存常见的登录信息,有时候爬取网站需要携带cookie信息访问,需要用到http.cookiejar,用于获取cookie以及储存cookie.

import http.cookiejar,urllib.request
cookie = http.cookiejar.CookieJar()
handler = urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(handler)
response = opener.open('http://www.baidu.com')
for item in cookie:
    print(item.name+"="+item.value)

BAIDUID=8BCB78D1239C83CD668FA5041F9DF6DE:FG=1
BIDUPSID=8BCB78D1239C83CD668FA5041F9DF6DE
H_PS_PSSID=26937_1454_21115_26350_20929
PSTM=1533277719
BDSVRTM=0
BD_HOME=0
delPer=0

可以使用两种方式http.cookiejar.MozillaCookieJar()和http.cookiejar.LWPCookieJar()将cookie保存到文件中。

#cookie保存_MozillaCookieJar
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)


# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This is a generated file!  Do not edit.

.baidu.com	TRUE	/	FALSE	3680761905	BAIDUID	131B96C4501657370132B6349EF07E9C:FG=1
.baidu.com	TRUE	/	FALSE	3680761905	BIDUPSID	131B96C4501657370132B6349EF07E9C
.baidu.com	TRUE	/	FALSE		H_PS_PSSID	1431_26911_21103_26923_22160
.baidu.com	TRUE	/	FALSE	3680761905	PSTM	1533278258
www.baidu.com	FALSE	/	FALSE		BDSVRTM	0
www.baidu.com	FALSE	/	FALSE		BD_HOME	0
www.baidu.com	FALSE	/	FALSE	2479358237	delPer	0

#cookie保存_LWPCookieJar
import http.cookiejar,urllib.request
filename = 'cookie.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_expires=True,ignore_discard=True)

#LWP-Cookies-2.0
Set-Cookie3: BAIDUID="5710DAB0383E5F73EE44B471E68D0FB5:FG=1"; path="/"; domain=".baidu.com"; path_spec; domain_dot; expires="2086-08-21 09:56:25Z"; version=0
Set-Cookie3: BIDUPSID=5710DAB0383E5F73EE44B471E68D0FB5; path="/"; domain=".baidu.com"; path_spec; domain_dot; expires="2086-08-21 09:56:25Z"; version=0
Set-Cookie3: H_PS_PSSID=1439_21100_20719; path="/"; domain=".baidu.com"; path_spec; domain_dot; discard; version=0
Set-Cookie3: PSTM=1533278539; path="/"; domain=".baidu.com"; path_spec; domain_dot; expires="2086-08-21 09:56:25Z"; version=0
Set-Cookie3: BDSVRTM=0; path="/"; domain="www.baidu.com"; path_spec; discard; version=0
Set-Cookie3: BD_HOME=0; path="/"; domain="www.baidu.com"; path_spec; discard; version=0
Set-Cookie3: delPer=0; path="/"; domain="www.baidu.com"; expires="2048-07-26 06:41:57Z"; version=0

如果需要使用文件中的cookie获取网页使用load方法,使用哪种方式写入就使用哪种方式读取。

#读取cookie
cookie = http.cookiejar.LWPCookieJar()
cookie.load('cookie.txt',ignore_discard=True,ignore_expires=True)
handler = urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(handler)
response = opener.open('http://www.baidu.com')
print(response.read().decode('utf-8'))

异常处理

URL可能产生的原因有以下几种

  • 网络无连接,即本机无法上网
  • 连接不到特定的服务器
  • 服务器不存在
#异常处理
from urllib import request,error
try:
    response = request.urlopen('http://pythonsite.com/1111.html')
except error.URLError as e:
    print(e.reason)

Not Found
from urllib import request,error
try:
    response = request.urlopen('http://www.baibai.com')
except error.URLError as e:
    print(e.reason)
[Errno 11002] getaddrinfo failed

这种错误包含一个错误号和一个错误信息。

urllib库中有两个异常,一个是URLError,另一个是HTTPError,其中HTTPError是URLError的子类。

URLError中只有一个属性:reason,即抓取异常时打印错误信息;

HTTPError中有三个属性:code,reason,headers,即抓取异常的时候可以获得code,reason,headers三个信息;

#HTTPError,URLError属性
from urllib import request,error
try:
    response = request.urlopen('http://pythonsite.com/1111.html')
except error.HTTPError as e:
    print(e.reason)
    print(e.code)
    print(e.headers)
except error.URLError as e:
    print(e.reason)
else:
    print('request successful')

Not Found
404
Date: Fri, 03 Aug 2018 07:01:53 GMT
Server: Apache
Vary: Accept-Encoding
Content-Length: 207
Connection: close
Content-Type: text/html; charset=iso-8859-1

HTTP状态码:HTTP协议所返回的响应状态。默认的处理器处理重定向(300以外号码),并且100-299范围的号码指示成功,所以只能看到400-599的错误号码。

  • 100:继续  客户端应当继续发送请求。客户端应当继续发送请求的剩余部分,或者如果请求已经完成,忽略这个响应。
  • 101: 转换协议  在发送完这个响应最后的空行后,服务器将会切换到在Upgrade 消息头中定义的那些协议。只有在切换新的协议更有好处的时候才应该采取类似措施。
  • 102:继续处理   由WebDAV(RFC 2518)扩展的状态码,代表处理将被继续执行。
  • 200:请求成功      处理方式:获得响应的内容,进行处理
  • 201:请求完成,结果是创建了新资源。新创建资源的URI可在响应的实体中得到    处理方式:爬虫中不会遇到
  • 202:请求被接受,但处理尚未完成    处理方式:阻塞等待
  • 204:服务器端已经实现了请求,但是没有返回新的信 息。如果客户是用户代理,则无须为此更新自身的文档视图。    处理方式:丢弃
  • 300:该状态码不被HTTP/1.0的应用程序直接使用, 只是作为3XX类型回应的默认解释。存在多个可用的被请求资源。    处理方式:若程序中能够处理,则进行进一步处理,如果程序中不能处理,则丢弃
  • 301:请求到的资源都会分配一个永久的URL,这样就可以在将来通过该URL来访问此资源    处理方式:重定向到分配的URL
  • 302:请求到的资源在一个不同的URL处临时保存     处理方式:重定向到临时的URL
  • 304:请求的资源未更新     处理方式:丢弃
  • 400:非法请求     处理方式:丢弃
  • 401:未授权     处理方式:丢弃
  • 403:禁止     处理方式:丢弃
  • 404:没有找到     处理方式:丢弃
  • 500:服务器内部错误  服务器遇到了一个未曾预料的状况,导致了它无法完成对请求的处理。一般来说,这个问题都会在服务器端的源代码出现错误时出现。
  • 501:服务器无法识别  服务器不支持当前请求所需要的某个功能。当服务器无法识别请求的方法,并且无法支持其对任何资源的请求。
  • 502:错误网关  作为网关或者代理工作的服务器尝试执行请求时,从上游服务器接收到无效的响应。
  • 503:服务出错   由于临时的服务器维护或者过载,服务器当前无法处理请求。这个状况是临时的,并且将在一段时间以后恢复。

同时可以对异常进行进一步的判断

#深入分析e.reason
import socket
from urllib import error,request
try:
    response = request.urlopen('http://www.pythonsite.com/',timeout=0.001)
except error.URLError as e:
    print(type(e.reason))
    if isinstance(e.reason,socket.timeout):
        print('time out')

<class 'socket.timeout'>
time out

URL解析

urlparse:用于分解URL String

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

from urllib.parse import urlparse
result = urlparse('http://www.baidu.com/index.html;user?id=5#comment')
print(result)

ParseResult(scheme='http', netloc='www.baidu.com', path='/index.html', params='user', query='id=5', fragment='comment')

也可以指定协议类型,但是如果url中有协议类型,会按照url中的类型,scheme中指定的协议类型就不会生效。

result = urlparse('http://www.baidu.com/index.html;user?id=5#comment',scheme='https')

ParseResult(scheme='http', netloc='www.baidu.com', path='/index.html', params='user', query='id=5', fragment='comment')

urlunparse:与urlparse的功能相反,用于拼接URL string

from urllib.parse import urlunparse
data = ['http','www.baidu.com','index.html','user','a=123','commit']
print(urlunparse(data))

http://www.baidu.com/index.html;user?a=123#commit

urljoin 拼接功能

from urllib.parse import urljoin
print(urljoin('http://www.baidu.com','FAQ.html'))
print(urljoin('http://www.baidu.com','https://pythonsite.com/FAQ.html'))
print(urljoin('http://www.baidu.com/about.html','https://pythonsite.com/FAQ.html'))
print(urljoin('http://www.baidu.com/about.html','https://pythonsite.com/FAQ.html?question=2'))
print(urljoin('http://www.baidu.com?wd=abc','https://pythonsite.com/index.php'))
print(urljoin('http://www.baidu.com','?category=2#comment'))
print(urljoin('www.baidu.com','?category=2#comment'))
print(urljoin('www.baidu.com','?category=2'))

http://www.baidu.com/FAQ.html
https://pythonsite.com/FAQ.html
https://pythonsite.com/FAQ.html
https://pythonsite.com/FAQ.html?question=2
https://pythonsite.com/index.php
http://www.baidu.com?category=2#comment
www.baidu.com?category=2#comment
www.baidu.com?category=2

拼接的时候后面的优先级高于前面的url

urlencode  将字典转换为url参数

from urllib.parse import urlencode
params = {
    'name':'flee',
    'age':21
}
base_url = 'http://www.baidu.com?'
url = base_url+urlencode(params)
print(url)

http://www.baidu.com?name=flee&age=21

https://www.cnblogs.com/zhaof/p/6910871.html

猜你喜欢

转载自blog.csdn.net/m0_38103546/article/details/81351110