上一篇简单的Demo确实实现了一些爬虫的功能。但是距真正的搜索引擎爬虫确实想去甚远。
1.首先下载URL时,大多是维护一个DNS服务器,找到相应的IP在进行下载网页。
2.维护URL队列时,上篇程序属于纵向的深度遍历,所以维护队列会越来越大,这算是比较大的bug了。解决方法起线程,或者每个页面只抓取一个URL。
3,关于URL抓取和种子URL写的也比较简陋,好吧,原谅他只是个Demo。
urllib2的库非常强大加入Header或者模仿cookies设置proxy等等。。。
先来说说urlopen()这个函数:
urllib2.urlopen(url[, data[, timeout[, cafile[, capath[, cadefault[, context]]]]])
Open the URL url, which can be either a string or a Request object.data may be a string specifying additional data to send to the server, or None if no such data is needed. Currently HTTP requests are the only ones that use data;
urlopen的返回值是个类文件对象,我们可以用read( ), info( ), getcode( ), geturl( )。
# -*- coding: utf-8 -*-
import urllib2
page = urllib2.urlopen("http://www.chinaz