您的位置:首页 > 理论基础 > 计算机网络

Python 网络爬虫 005 (编程) 如何编写一个可以 下载(或叫:爬取)一个网页 的网络爬虫

2016-09-09 18:16 991 查看

如何编写一个可以 下载(或叫:爬取)一个网页 的网络爬虫

使用的系统:Windows 10 64位

Python 语言版本:Python 2.7.10 V

使用的编程 Python 的集成开发环境:PyCharm 2016 04

我使用的 urllib 的版本:urllib2

注意: 我没这里使用的是 Python2 ,而不是Python3

一 . 简介

编写网络爬虫的第一步就是下载网页,这个过程叫做: 爬取

二 . 准备工作

要想爬取网站,首先要将网站下载下来。我们使用 Python 内置的:urllib2 模块 来下载网站的 URL

注意: 无须下载任何第三方库哦。

参考网站:http://stackoverflow.com/questions/34475051/need-to-install-urllib2-for-python-3-5-1

三 . 最简单的下载网页的网络爬虫程序

新建一个文件:
download_html.py
。将下面的代码输入进去:

import urllib2
def download(url):
return urllib2.urlopen(url).read()


执行:

> C:\Python27\python.exe -i download_html.py
>>> html = download('http://www.aobosir.com')
>>> print html
<!DOCTYPE html>
<html>
<head>

</head>
<body>

</body>

<script>
window.location.href="http://blog.csdn.net/github_35160620"
</script>
</html>

>>>


这样的程序,不是每一次都很顺利的运行的。我们可能会在执行上面这个程序的时候,遇到一些无法控制的问题,这根据你爬取的网站的不同而定。

* 比如,有的网站使用的编码方式与你这个程序下载网站是读取网站的编码方式不同,就会导致乱码问题;

* 再比如说:如果你爬取的目标网站具有反爬虫机制,那么当你爬取它的时候,你会遇到
503 Service Unavailable
问题;

* 再比如说:如果你爬虫的网站根本不存在,那么你会得到
404 Not Found
问题。

四 . 加入异常处理程序

所以,我们为了能让程序捕获到这些异常情况,需要加上异常处理的程序。最终程序会变成下面这个样子:

import urllib2

def download(url):
print 'Downloading: ', url
try:
html = urllib2.urlopen(url).read()
except urllib2.URLError as e:
print 'Download error', e.reason
html = None
return html


运行:

D:\WorkSpace\python_ws\web-scraping-with-python-learning\ch1>C:\Python27\python.exe -i 1-4-1-download_html.py
>>> html = download('http://www.aobosir.com/')
Downloading:  http://www.aobosir.com/ >>> html = download('http://httpstat.us/500')
Downloading:  http://httpstat.us/500 Download error Internal Server Error
>>> html = download('http://www.meetup.com/')
Downloading:  http://www.meetup.com/ Download error Forbidden
>>>


现在,如果程序在下载目标网站时,遇到异常就能捕获到这个异常了,然后返回
None
。现在还没有处理这个异常。只是能过捕获到它。

当然,下载网页是,遇到的错误经常都是临时性的,比如对于我们上面说的
503
问题,我们只需要尝试重新下载就可以解决这个问题。当然了,我们不需要对所有问题都尝试去重新下载,比如
404
问题,目标网页根本不存在,所以,你没有必要再去尝试。

扩展:

互联网工程任务小组(英语:Internet Engineering Task Force,缩写为 IETF)定义了HTTP错误的完整列表,详细内容到这个网站查看:https://zh.wikipedia.org/wiki/HTTP%E7%8A%B6%E6%80%81%E7%A0%81

从这个列表中,我们可以了解到:
4XX
问题是客户端出现的问题;
5XX
问题是服务器出现了问题;而其他的,比如:
1XX
是消息;
2XX
是成功;
3XX
是重定向。

五 . 只有出现
5xx
错误码的时候,才执行重新下载程序

所以,我们需要让
download()
这个函数在遇到
5XX
错误的时候重新下载即可。

所以,我们再次完善我们的
download()
函数:

import urllib2

def download(url, num_retries=2):
print 'Downloading: ', url
try:
html = urllib2.urlopen(url).read()
except urllib2.URLError as e:
print 'Download error', e.reason
html = None
if num_retries > 0:
if hasattr(e, 'code') and 500 <= e.code < 600:
# recursively retry 5xx HTTP errors
return download(url, num_retries-1)
return html


现在,在
download()
函数遇到
5xx
错误码的时候,就会自动递归
download()
函数本身进行重试。
download()
函数增加的第2个参数就是迎来指定递归的次数的,默认尝试2次。

测试这段程序,使用这个网站来测试:http://httpstat.us/500 ,这个网站会始终返回
500
错误码:

>>> html = download('http://httpstat.us/500')
Downloading:  http://httpstat.us/500 Download error Internal Server Error
Downloading:  http://httpstat.us/500 Download error Internal Server Error
Downloading:  http://httpstat.us/500 Download error Internal Server Error
>>> html = download('http://www.meetup.com/')
Downloading:  http://www.meetup.com/ Download error Forbidden
>>>


六 . 设置 用户代理(
user_agent

为了让我们的网络爬虫更加健壮,在网络中下载网页更加畅通无阻,我们需要:设置 用户代理(
user_agent


Q: 什么是 用户代理(
user_agent
)?

A: 你访问目标网站,就是访问目标服务器。那么让你访问服务器的时候,服务器会读取你是使用什么来访问它的。比如:如果你使用是是Chrome 浏览器访问它的,那么目标服务器就知道:你是使用浏览器访问它的。服务器就是通过用户代理这个参数来得到你是通过什么访问它的。

用户代理 指的是:简单的说就是一个参数,并且这个参数我们是可以修改的。在默认情况下,我们使用urllib2写的爬虫,它默认将 Python-urllib/2.7 这个字符串做为爬虫程序的用户代理

Q: 为什么要设置 用户代理(
user_agent
)?

A: 如果我们使用这个默认的 Python-urllib/2.7 这个字符串做为我们的爬虫程序的用户代理,我们的爬虫会碰到很多的问题的。有些网站为了不希望网络爬虫将它们的服务器搞过载 或者 搞垮,会设有反爬虫机制。反爬虫机制就是:将一些用户代理 封禁,禁止一些 用户代理 访问他们的服务器。

比如这个例子:在我们使用urllib2 默认的用户代理Python-urllib/2.7 ) 来 http://www.meetup.com/ 网站,你会看到下面的访问拒绝提示:

>>> html = download('http://blog.csdn.net/github_35160620/article/details/52487618')
Downloading:  http://blog.csdn.net/github_35160620/article/details/52487618 Download error Forbidden
>>> html = download('http://www.meetup.com/')
Downloading:  http://www.meetup.com/ Download error Forbidden
>>>


所以,如果我们能过将网络爬虫用户代理 设置为 浏览器的用户代理,该多好啊。这样我们的网络爬虫就可以让服务器误以为:是一个浏览器在访问它。

下面这个图,就是 Chrome 浏览器的 用户代理
user_agent
)。



这是完全可以实现的,下面我们将来介绍,如何设定。

现在修改
download()
函数,将用户代理 设定为:
wswp
这个字符串。(即 Web Scraping with Python 的首字母缩写。)

import urllib2

def download(url, user_agent='wswp', num_retries=2):
print 'Downloading: ', url
headers = {'User-agent' : user_agent}
request = urllib2.Request(url, headers=headers)
try:
html = urllib2.urlopen(request).read()
except urllib2.URLError as e:
print 'Download error', e.reason
html = None
if num_retries > 0:
if hasattr(e, 'code') and 500 <= e.code < 600:
# recursively retry 5xx HTTP errors
return download(url, user_agent, num_retries-1)
return html


运行:

>>> html = download('http://www.meetup.com/')
Downloading:  http://www.meetup.com/[/code] 
通过运行结果,你也看到了:通过设置了自定义的用户代理
user_agent
)后,一些反爬虫的网站有可以下载了。现在,这个
download()
就是一个灵活的下载程序,该函数能够捕获异常,并且可以自动尝试重新下载,并且我们设置了自定义的用户代理
user_agent
)。

搞定

下一节,我们来解决乱码问题

如果我们下载一个带有中文的网站,或者日文的网站,终止就是不全是英文的网站。我们将这个网站下载下来后,将它输出,会有乱码。

举例:

>>> html = download('http://blog.csdn.net/github_35160620/article/details/52487618')
Downloading:  http://blog.csdn.net/github_35160620/article/details/52487618 >>> print html
...
...
...
</div>

<!-- 骞垮憡浣嶅紑濮?-->
<ins data-revive-zoneid="72" data-revive-id="8c38e720de1c90a6f6ff52f3f89c4d57"></ins>
<!-- 骞垮憡浣嶇粨鏉?-->

<link rel="stylesheet" href="http://static.blog.csdn.net/css/blog_code.css" />
<script type="text/javascript" src="http://static.blog.csdn.net/scripts/saveToCode.js"></script>
...
...
...


下一节,就来解决这个问题。

总结:

这一节,我们已经 网络爬虫程序的下载网页的部分搞定了。下一节,我们来解决乱码问题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐