1.urllib2简介

urllib2的是爬取URL(统一资源定位器)的Python模块。它提供了一个非常简单的接口,使用urlopen函数。它能够使用多种不同的协议来爬取URL。
它还提供了一个稍微复杂的接口,用于处理常见的情况 - 如基本身份验证,cookies,代理等。

2.抓取URLs

使用urlib2的简单方式可以像下面一样:

import urllib2
response = urllib2.urlopen('http://python.org/')
html = response.read()
print html

输出就是爬取的网页内容。

我们可以使用urllib2抓取格式形式的url,可以将‘http:’用‘ftp:’,‘file:’等代替。http是基于请求应答模式,urllib2使用Request代表HTTP请求,最简单的形式是创建一个Request对象,指定要获取的URL。使用Request对象调用urlopen,返回一个请求的URL

响应对象。此响应是一个类似文件的对象,这意味着你可以对这个对象使用.read():

import urllib2
req = urllib2.Request('http://www.voidspace.org.uk')
response = urllib2.urlopen(req)
the_page = response.read()
print the_page

urlib2可以使用各种URL模式,例如可以使用ftp形式:

req = urllib2.Request('ftp://example.com/')

3.Data

有时你想将数据发送到一个URL(通常是URL将指向一个CGI(通用网关接口)脚本或其他Web应用程序)。

通过HTTP,这通常使用一个POST请求。 这是当你提交你填写的HTML表单,浏览器通常使用POST请求。

并非所有POST都都来源于表单:你可以使用一个POST传送任意数据到自己的应用程序。

在通常情况下HTML表单,需要对数据编码成标准方式,然后传递到请求对象作为数据参数。编码是使用的函数来自urllib库不是从urllib2的。

import urllib
import urllib2
url = 'http://www.someserver.com/cgi-bin/register.cgi'
values = {'name' : 'Michael Foord',
'location' : 'Northampton',
'language' : 'Python' }
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()

如果你没有提交data参数,urllib2使用GET请求。GET和POST请求不同之处在于POST请求通常有“副作用”:他们以某种方式改变了系统的状态。

虽然HTTP标准明确规定,POST可能会引起副作用,而GET请求从来没有引起副作用,data也可以在HTTP GET请求通过在URL本身编码来传送。

>>> import urllib2
>>> import urllib
>>> data = {}
>>> data['name'] = 'Somebody Here'
>>> data['location'] = 'Northampton'
>>> data['language'] = 'Python'
>>> url_values = urllib.urlencode(data)
>>> print url_values # The order may differ.
name=Somebody+Here&language=Python&location=Northampton
>>> url = 'http://www.example.com/example.cgi'
>>> full_url = url + '?' + url_values
>>> data = urllib2.urlopen(full_url)

全的URL需要加一个?在URL后面,后面跟着encoded values。

4 Headers

我们将在这里讨论一个特定的HTTP头,来说明如何headers添加到您的HTTP请求。有些网站不喜欢被程序浏览,或发送不同的版本内容到不同的浏览器。

urllib2默认的自身标识为Python-urllib/ XY(x和y是Python主版本和次版本号,例如Python-urllib/2.5),这可能会使网站迷惑,或只是简单的不能正常工作。

浏览器通过User-Agent标识自己,当你创建一个Request对象,你可以传送一个包含头部的字典。

下面的例子标题的字典作出了和上面同样的要求,但自身标识为 Internet Explorer 5 。

import urllib
import urllib2
url = 'http://www.someserver.com/cgi-bin/register.cgi'
user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)'
values = {'name' : 'Michael Foord',
'location' : 'Northampton',
'language' : 'Python' }
headers = { 'User-Agent' : user_agent }
data = urllib.urlencode(values)
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
the_page = response.read()

5 URLError

urlopen不能处理的响应时(通常的Python APIs异常如ValueError,TypeError等也会同时产生)他会引发URLError。

HTTPError是URLError的子类,一般在特定的HTTP URL中产生。

通常,URLError产生是因为没有网络连接(到指定的服务器的路由),或指定的服务器不存在。在这种情况下,所提出的异常将有一个“reason”属性,它是含有一个元组包含错误代码和文本错误消息。

import urllib2
req = urllib2.Request('http://www.pretend_server.org')
try:
urllib2.urlopen(req)
except urllib2.URLError as e:
print e.reason

输出是:

[Errno -2] Name or service not known

6 HTTPError

来自服务器的HTTP响应包含一个数字“状态码”。

有时,状态代码表示服务器无法完成请求。默认处理程序将处理一些这类的响应(例如,如果该响应是一个“重定向”,请求客户端从不同的URL获取文档,urllib2将会处理)。

对于那些它不能处理,urlopen会引发HTTPError。典型错误包括“404”(找不到网页),“403”(要求禁止),和'401'(需要身份验证)。

下面是Error Codes

# Table mapping response codes to messages; entries have the
# form {code: (shortmessage, longmessage)}.
responses = {
100: ('Continue', 'Request received, please continue'),
101: ('Switching Protocols',
'Switching to new protocol; obey Upgrade header'),
200: ('OK', 'Request fulfilled, document follows'),
201: ('Created', 'Document created, URL follows'),
202: ('Accepted',
'Request accepted, processing continues off-line'),
203: ('Non-Authoritative Information', 'Request fulfilled from cache'),
204: ('No Content', 'Request fulfilled, nothing follows'),
205: ('Reset Content', 'Clear input form for further input.'),
206: ('Partial Content', 'Partial content follows.'),
300: ('Multiple Choices',
'Object has several resources -- see URI list'),
301: ('Moved Permanently', 'Object moved permanently -- see URI list'),
302: ('Found', 'Object moved temporarily -- see URI list'),
303: ('See Other', 'Object moved -- see Method and URL list'),
304: ('Not Modified',
'Document has not changed since given time'),
305: ('Use Proxy',
'You must use proxy specified in Location to access this '
'resource.'),
307: ('Temporary Redirect',
'Object moved temporarily -- see URI list'),
400: ('Bad Request',
'Bad request syntax or unsupported method'),
401: ('Unauthorized',
'No permission -- see authorization schemes'),
402: ('Payment Required',
'No payment -- see charging schemes'),
403: ('Forbidden',
'Request forbidden -- authorization will not help'),
404: ('Not Found', 'Nothing matches the given URI'),
405: ('Method Not Allowed',
'Specified method is invalid for this server.'),
406: ('Not Acceptable', 'URI not available in preferred format.'),
407: ('Proxy Authentication Required', 'You must authenticate with '
'this proxy before proceeding.'),
408: ('Request Timeout', 'Request timed out; try again later.'),
409: ('Conflict', 'Request conflict.'),
410: ('Gone',
'URI no longer exists and has been permanently removed.'),
411: ('Length Required', 'Client must specify Content-Length.'),
412: ('Precondition Failed', 'Precondition in headers is false.'),
413: ('Request Entity Too Large', 'Entity is too large.'),
414: ('Request-URI Too Long', 'URI is too long.'),
415: ('Unsupported Media Type', 'Entity body in unsupported format.'),
416: ('Requested Range Not Satisfiable',
'Cannot satisfy request range.'),
417: ('Expectation Failed',
'Expect condition could not be satisfied.'),
500: ('Internal Server Error', 'Server got itself in trouble'),
501: ('Not Implemented',
'Server does not support this operation'),
502: ('Bad Gateway', 'Invalid responses from another server/proxy.'),
503: ('Service Unavailable',
'The server cannot process the request due to a high load'),
504: ('Gateway Timeout',
'The gateway server did not receive a timely response'),
505: ('HTTP Version Not Supported', 'Cannot fulfill request.'),
}

当错误被返回一个HTTP错误代码和错误页面提高服务器响应。您可以使用为页面上的响应HTTPError这样的实例返回。这意味着,以及代码属性,它也有阅读中,getURL和信息,方法。
当一个错误号产生后,服务器会返回一个HTTP错误号和一个错误页面。
可以使用HTTPError实例作为页面返回的response应答对象。
这表示和错误属性一样,它同样包含了read,geturl,和info方法。

import urllib2
req = urllib2.Request('http://www.python.org/fish.html')
try:
urllib2.urlopen(req)
except urllib2.HTTPError as e:
print e.code
print e.read()

运行发现:

404
<!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>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">

<link rel="prefetch" href="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js">

<meta name="application-name" content="Python.org">
。。。。。。。

Python爬虫学习笔记(一)的更多相关文章

  1. python爬虫学习笔记(一)——环境配置(windows系统)

    在进行python爬虫学习前,需要进行如下准备工作: python3+pip官方配置 1.Anaconda(推荐,包括python和相关库)   [推荐地址:清华镜像] https://mirrors ...

  2. Python爬虫学习笔记(三)

    Cookies: 以抓取https://www.yaozh.com/为例 Test1(不使用cookies): 代码: import urllib.request # 1.添加URL url = &q ...

  3. python爬虫学习笔记

    爬虫的分类 1.通用爬虫:通用爬虫是搜索引擎(Baidu.Google.Yahoo等)“抓取系统”的重要组成部分.主要目的是将互联网上的网页下载到本地,形成一个互联网内容的镜像备份. 简单来讲就是尽可 ...

  4. Python、pip和scrapy的安装——Python爬虫学习笔记1

    Python作为爬虫语言非常受欢迎,近期项目需要,很是学习了一番Python,在此记录学习过程:首先因为是初学,而且当时要求很快速的出demo,所以首先想到的是框架,一番查找选用了Python界大名鼎 ...

  5. 一入爬虫深似海,从此游戏是路人!总结我的python爬虫学习笔记!

    前言 还记得是大学2年级的时候,偶然之间看到了学长在学习python:我就坐在旁边看他敲着代码,感觉很好奇.感觉很酷,从那之后,我就想和学长一样的厉害,就想让学长教我,请他吃了一周的饭,他答应了.从此 ...

  6. Python爬虫学习笔记——豆瓣登陆(一)

    #-*- coding:utf-8 -*- import requests from bs4 import BeautifulSoup import html5lib import re import ...

  7. Python爬虫学习笔记-1.Urllib库

    urllib 是python内置的基本库,提供了一系列用于操作URL的功能,我们可以通过它来做一个简单的爬虫. 0X01 基本使用 简单的爬取一个页面: import urllib2 request ...

  8. 【Python爬虫学习笔记(3)】Beautiful Soup库相关知识点总结

    1. Beautiful Soup简介     Beautiful Soup是将数据从HTML和XML文件中解析出来的一个python库,它能够提供一种符合习惯的方法去遍历搜索和修改解析树,这将大大减 ...

  9. 【Python爬虫学习笔记(1)】urllib2库相关知识点总结

    1. urllib2的opener和handler概念 1.1 Openers: 当你获取一个URL你使用一个opener(一个urllib2.OpenerDirector的实例).正常情况下,我们使 ...

  10. Python爬虫学习笔记(一)

    概念: 使用代码模拟用户,批量发送网络请求,批量获取数据. 分类: 通用爬虫: 通用爬虫是搜索引擎(Baidu.Google.Yahoo等)"抓取系统"的重要组成部分. 主要目的是 ...

随机推荐

  1. 30分钟Git命令“从入门到放弃”

    git 现在的火爆程度非同一般,它被广泛地用在大型开源项目中,但是初学者非常容易“从入门到放弃”,各种命令各种参数,天哪,宝宝要吓哭了.实际上新手并不需要了解所有命令的用途,学习是需要一个循序渐进的过 ...

  2. [HTML5] Track First Contentful Paint with PerformanceObserver and Google Analytics

    "If you can't measure it, you can't improve it." The first step when doing performance wor ...

  3. ffmpeg对rtmp的基本操作[转]

    ffplay和ffmpeg调用的库是一样的.我刚才试了一下,是可以播的.ffplay "rtmp://tsl.s1979.cutv.com:1935/cutvChannelLive/AxeF ...

  4. OkDownload项目实战

    本文介绍项目中引入okhttp-okgo开源框架里的OkDownload部分,实现了RecyclerView列表的下载功能. 引入OKDownload 需求不仅是要支持断点续传,而且还要支持队列下载和 ...

  5. 算法笔记_166:算法提高 金属采集(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 人类在火星上发现了一种新的金属!这些金属分布在一些奇怪的地方,不妨叫它节点好了.一些节点之间有道路相连,所有的节点和道路形成了一棵树.一共 ...

  6. pip运行报错Fatal error in launcher: Unable to create process using pip.exe

    使用pip的时候报错Fatal error in launcher: Unable to create process using pip.exe 解决办法,升级pip python -m pip i ...

  7. Intellij IDEA 10.5 语言设置

    适应于:英文操作系统,但是语言和区域设置为中文的环境. Mac: /Applications/IntelliJ IDEA CE.app/Contents/bin/idea.vmoptions 增加 - ...

  8. 【Linux】监控系统的状态

    1.w命令 w命令是一个很强大的命令,该命令显示的信息比较丰富.以下是我的虚拟机w命令的一个展示 从上图我们可以看到: 第一行从左面开始显示的信息依次为:时间.系统运行时间.登陆用户数.平均负载 第二 ...

  9. shell脚本对多端口进程kill并重启进程

    #!/bin/bash export LOG_FILE='/data/log/search' spider_search_pid=`ps aux|grep flask_web_search|grep ...

  10. 比较R语言、perl语言,matlab中for循环和while循环的使用

    http://zhan.renren.com/zxccshkbx?gid=3602888498030523562&from=post&checked=true