安裝部署Scrapy
在安裝Scrapy前首先需要確定的是已經安裝好了Python(目前Scrapy支持Python2.5,Python2.6和Python2.7)。官方文檔中介紹了三種方法進行安裝,我采用的是使用 easy_install 進行安裝,首先是下載Windows版本的setuptools(下載地址:http://pypi.python.org/pypi/setuptools),下載完后一路NEXT就可以了。
安裝完setuptool以后。執行CMD,然后運行一下命令:
easy_install -U Scrapy
同樣的你可以選擇使用pip安裝,pip的地址:http://pypi.python.org/pypi/pip
使用pip安裝Scrapy的命令為
pip install Scrapy
如果你的電腦先前裝過visual studio 2008 或 visual studio 2010那么一起順利,Scrapy已經安裝完成。如果出現下列報錯:Unable to find vcvarsall.bat 那么你需要折騰下。你可以安裝visual studio 后進行安裝或采用下面的方式進行解決:
首先安裝MinGW(MinGW下載地址:http://sourceforge.net/projects/mingw/files/),在MinGW的安裝目錄下找到bin的文件夾,找到mingw32-make.exe,復制一份更名為make.exe;
把MinGW的路徑添加到環境變量path中,比如我把MinGW安裝到D:/MinGW/中,就把D:/MinGW/bin添加到path中;
打開命令行窗口,在命令行窗口中進入到要安裝代碼的目錄下;
輸入如下命令 setup.py install build –compiler=mingw32 就可以安裝了。
如果出現“xslt-config' 不是內部或外部命令,也不是可運行的程序或批處理文件。”錯誤,原因主要是lxml安裝不成功,只要上http://pypi.python.org/simple/lxml/下載個exe文件進行安裝就可以了。
下面就可以進入正題了。
新建工程
讓我們來用爬蟲獲取豆瓣電影Top 250的電影信息吧。開始之前,我們新建一個Scrapy工程。因為我用的Win7,所以在CMD中進入一個我希望保存代碼的目錄,然后執行:
D:/WEB/Python>scrapy startproject doubanmoive
這個命令會在當前目錄下創建一個新的目錄doubanmoive,目錄結構如下:
D:/WEB/Python/doubanmoive>tree /fFolder PATH listing for volume DataVolume serial number is 00000200 34EC:9CB9D:.│ scrapy.cfg│└─doubanmoive │ items.py │ pipelines.py │ settings.py │ __init__.py │ └─spiders __init__.py
這些文件主要為:
- doubanmoive/items.py: 定義需要獲取的內容字段,類似于實體類。
- doubanmoive/pipelines.py: 項目管道文件,用來處理Spider抓取的數據。
- doubanmoive/settings.py: 項目配置文件
- doubanmoive/spiders: 放置spider的目錄
定義項目(Item)
Item是用來裝載抓取數據的容器,和Java里的實體類(Entity)比較像,打開doubanmoive/items.py可以看到默認創建了以下代碼。
from scrapy.item import Item, Fieldclass DoubanmoiveItem(Item): pass
我們只需要在 Doubanmoive 類中增加需要抓取的字段即可,如 name=Field() ,最后根據我們的需求完成代碼如下。
from scrapy.item import Item, Fieldclass DoubanmoiveItem(Item): name=Field()#電影名 year=Field()#上映年份 score=Field()#豆瓣分數 director=Field()#導演 classification=Field()#分類 actor=Field()#演員
編寫爬蟲(Spider)
Spider是整個項目中最核心的類,在這個類里我們會定義抓取對象(域名、URL)以及抓取規則。Scrapy官方文檔中的教程是基于 BaseSpider 的,但 BaseSpider 只能爬取給定的URL列表,無法根據一個初始的URL向外拓展。不過除了 BaseSpider ,還有很多可以直接繼承 Spider 的類,比如 scrapy.contrib.spiders.CrawlSpider 。
在 doubanmoive/spiders 目錄下新建moive_spider.py文件,并填寫代碼。
# -*- coding: utf-8 -*-from scrapy.selector import Selectorfrom scrapy.contrib.spiders import CrawlSpider,Rulefrom scrapy.contrib.linkextractors.sgml import SgmlLinkExtractorfrom doubanmoive.items import DoubanmoiveItemclass MoiveSpider(CrawlSpider): name="doubanmoive" allowed_domains=["movie.douban.com"] start_urls=["http://movie.douban.com/top250"] rules=[ Rule(SgmlLinkExtractor(allow=(r'http://movie.douban.com/top250/?start=/d+.*'))), Rule(SgmlLinkExtractor(allow=(r'http://movie.douban.com/subject//d+')),callback="parse_item"), ] def parse_item(self,response): sel=Selector(response) item=DoubanmoiveItem() item['name']=sel.xpath('//*[@id="content"]/h1/span[1]/text()').extract() item['year']=sel.xpath('//*[@id="content"]/h1/span[2]/text()').re(r'/((/d+)/)') item['score']=sel.xpath('//*[@id="interest_sectl"]/div/p[1]/strong/text()').extract() item['director']=sel.xpath('//*[@id="info"]/span[1]/a/text()').extract() item['classification']= sel.xpath('//span[@property="v:genre"]/text()').extract() item['actor']= sel.xpath('//*[@id="info"]/span[3]/a[1]/text()').extract() return item
代碼說明: MoiveSpider 繼承Scrapy中的 CrawlSpider , name , allow_domains , start_url 看名字就知道什么含義,其中rules稍微復雜一些,定義了URL的抓取規則,符合 allow 正則表達式的鏈接都會加入到Scheduler(調度程序)。通過分析豆瓣電影Top250的分頁URL http://movie.douban.com/top250?start=25&filter=&type= 可以得到以下規則
Rule(SgmlLinkExtractor(allow=(r'http://movie.douban.com/top250/?start=/d+.*'))),
而我們真正要抓取的頁面是每一個電影的詳細介紹,如肖申克的救贖的鏈接為 http://movie.douban.com/subject/1292052/ ,那只有 subject 后面的數字是變化的,根據正則表達式得到如下代碼。我們需要抓取這種類型鏈接中的內容,于是加入callback屬性,將Response交給parse_item函數來處理。
Rule(SgmlLinkExtractor(allow=(r'http://movie.douban.com/subject//d+')),callback="parse_item"),
在 parse_item 函數中的處理邏輯非常簡單,獲取符合條件鏈接的代碼,然后根據一定的規則抓取內容賦給item并返回 Item Pipeline 。獲取大部分標簽的內容不需要編寫復雜的正則表達式,我們可以使用 XPath 。 XPath 是一門在 XML 文檔中查找信息的語言,但它也可以用在HTML中。下表列出了常用表達式。
表達式 | 描述 |
---|---|
nodename | 選取此節點的所有子節點。 |
/ | 從根節點選取。 |
// | 從匹配選擇的當前節點選擇文檔中的節點,而不考慮它們的位置。 |
. | 選取當前節點。 |
.. | 選取當前節點的父節點。 |
@ | 選取屬性。 |
如 //*[@id="content"]/h1/span[1]/text() 獲取的結果是在id為content的任意元素下h1元素下的span列表中第一個元素的文本內容。我們可以通過Chrome開發者工具(F12)來獲取某內容的XPath表達式,具體操作為在需要抓取的內容上點擊審查元素,下方就會出現開發者工具,并定位到該元素,在內容上點擊右鍵,選擇復制XPath。
存儲數據
爬蟲獲取到數據以后我們需要將其存儲到數據庫中,之前我們提到該操作需要靠項目管道(pipeline)來處理,其通常執行的操作為:
- 清洗HTML數據
- 驗證解析到的數據(檢查項目是否包含必要的字段)
- 檢查是否是重復數據(如果重復就刪除)
- 將解析到的數據存儲到數據庫中
由于我們獲取的數據格式多種多樣,有一些存儲在關系型數據庫中并不方便,所以我在寫完MySQL版本的Pipeline之后又寫了一個MongoDB的。
MySQL版本:
# -*- coding: utf-8 -*-from scrapy import logfrom twisted.enterprise import adbapifrom scrapy.http import Requestimport MySQLdbimport MySQLdb.cursorsclass DoubanmoivePipeline(object): def __init__(self): self.dbpool = adbapi.ConnectionPool('MySQLdb', db = 'python', user = 'root', passwd = 'root', cursorclass = MySQLdb.cursors.DictCursor, charset = 'utf8', use_unicode = False ) def process_item(self, item, spider): query = self.dbpool.runInteraction(self._conditional_insert, item) query.addErrback(self.handle_error) return item def _conditional_insert(self,tx,item): tx.execute("select * from doubanmoive where m_name= %s",(item['name'][0],)) result=tx.fetchone() log.msg(result,level=log.DEBUG) print result if result: log.msg("Item already stored in db:%s" % item,level=log.DEBUG) else: classification=actor='' lenClassification=len(item['classification']) lenActor=len(item['actor']) for n in xrange(lenClassification): classification+=item['classification'][n] if n<lenClassification-1: classification+='/' for n in xrange(lenActor): actor+=item['actor'][n] if n<lenActor-1: actor+='/' tx.execute(/ "insert into doubanmoive (m_name,m_year,m_score,m_director,m_classification,m_actor) values (%s,%s,%s,%s,%s,%s)",/ (item['name'][0],item['year'][0],item['score'][0],item['director'][0],classification,actor)) log.msg("Item stored in db: %s" % item, level=log.DEBUG) def handle_error(self, e): log.err(e)
MongoDB版本:
# -*- coding: utf-8 -*-import pymongofrom scrapy.exceptions import DropItemfrom scrapy.conf import settingsfrom scrapy import logclass MongoDBPipeline(object): #Connect to the MongoDB database def __init__(self): connection = pymongo.Connection(settings['MONGODB_SERVER'], settings['MONGODB_PORT']) db = connection[settings['MONGODB_DB']] self.collection = db[settings['MONGODB_COLLECTION']] def process_item(self, item, spider): #Remove invalid data valid = True for data in item: if not data: valid = False raise DropItem("Missing %s of blogpost from %s" %(data, item['url'])) if valid: #Insert data into database new_moive=[{ "name":item['name'][0], "year":item['year'][0], "score":item['score'][0], "director":item['director'], "classification":item['classification'], "actor":item['actor'] }] self.collection.insert(new_moive) log.msg("Item wrote to MongoDB database %s/%s" % (settings['MONGODB_DB'], settings['MONGODB_COLLECTION']), level=log.DEBUG, spider=spider) return item
可以看到其基本的處理流程是一樣,但是MySQL不太方便的一點就是需要將數組類型的數據通過分隔符轉換。而MongoDB支持存入List、Dict等多種類型的數據。
配置文件
在運行爬蟲之前還需要將在 settings.py 中增加一些配置信息。
BOT_NAME = 'doubanmoive'SPIDER_MODULES = ['doubanmoive.spiders']NEWSPIDER_MODULE = 'doubanmoive.spiders'ITEM_PIPELINES={ 'doubanmoive.mongo_pipelines.MongoDBPipeline':300, 'doubanmoive.pipelines.DoubanmoivePipeline':400,}LOG_LEVEL='DEBUG'DOWNLOAD_DELAY = 2RANDOMIZE_DOWNLOAD_DELAY = TrueUSER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.54 Safari/536.5'COOKIES_ENABLED = TrueMONGODB_SERVER = 'localhost'MONGODB_PORT = 27017MONGODB_DB = 'python'MONGODB_COLLECTION = 'test'
ITEM_PIPELINES 中定義了MySQL和MongoDB兩個Pipeline文件,后面的數字代表執行的優先級順序,范圍為0~1000。 而中間的 DOWNLOAD_DELAY 等信息是為了防止爬蟲被豆瓣Ban掉,增加了一些隨機延遲,瀏覽器代理等。最后的就是MongoDB的配置信息,MySQL也可以參考這種方式來寫。
至此為止,抓取豆瓣電影的爬蟲就已經完成了。在命令行中執行 Scrapy crawl doubanmoive 讓蜘蛛開始爬行吧!