《Scrapy的架构初探》一文讲解了Scrapy的架构,本文就实际来安装运行一下Scrapy爬虫。本文以官网的tutorial做为例子,完整的代码能够在github上下载。html
本次测试的环境是:Windows10, Python3.4.3 32bitpython
安装Scrapy : $ pip install Scrapy #实际安装时,因为服务器状态的不稳定,出现好几回中途退出的状况git
3.1. 生成一个新项目:tutorialgithub
$ scrapy startproject tutorial
项目目录结构以下:json
3.2. 定义要抓取的itemsegmentfault
# -*- coding: utf-8 -*- # Define here the models for your scraped items # # See documentation in: # http://doc.scrapy.org/en/latest/topics/items.html import scrapy class DmozItem(scrapy.Item): title = scrapy.Field() link = scrapy.Field() desc = scrapy.Field()
3.3. 定义Spiderapi
import scrapy from tutorial.items import DmozItem class DmozSpider(scrapy.Spider): name = "dmoz" allowed_domains = ["dmoz.org"] start_urls = [ "http://www.dmoz.org/Computers/Programming/Languages/Python/Books/", "http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/" ] def parse(self, response): for sel in response.xpath('//ul/li'): item = DmozItem() item['title'] = sel.xpath('a/text()').extract() item['link'] = sel.xpath('a/@href').extract() item['desc'] = sel.xpath('text()').extract() yield item
3.4. 运行服务器
$ scrapy crawl dmoz -o item.json
1) 结果报错:
A) ImportError: cannot import name '_win32stdio'
B) ImportError: No module named 'win32api'网络
2) 查错过程:查看官方的FAQ和stackoverflow上的信息,原来是scrapy在python3上测试还不充分,还有小问题。架构
3) 解决过程:
A) 须要手工去下载twisted/internet下的 _win32stdio 和 _pollingfile,存放到python目录的libsitepackagestwistedinternet下
B) 下载并安装pywin32
再次运行,成功!在控制台上能够看到scrapy的输出信息,待运行完成退出后,到项目目录打开结果文件items.json, 能够看到里面以json格式存储的爬取结果。
[ {"title": [" About "], "desc": [" ", " "], "link": ["/docs/en/about.html"]}, {"title": [" Become an Editor "], "desc": [" ", " "], "link": ["/docs/en/help/become.html"]}, {"title": [" Suggest a Site "], "desc": [" ", " "], "link": ["/docs/en/add.html"]}, {"title": [" Help "], "desc": [" ", " "], "link": ["/docs/en/help/helpmain.html"]}, {"title": [" Login "], "desc": [" ", " "], "link": ["/editors/"]}, {"title": [], "desc": [" ", " Share via Facebook "], "link": []}, {"title": [], "desc": [" ", " Share via Twitter "], "link": []}, {"title": [], "desc": [" ", " Share via LinkedIn "], "link": []}, {"title": [], "desc": [" ", " Share via e-Mail "], "link": []}, {"title": [], "desc": [" ", " "], "link": []}, {"title": [], "desc": [" ", " "], "link": []}, {"title": [" About "], "desc": [" ", " "], "link": ["/docs/en/about.html"]}, {"title": [" Become an Editor "], "desc": [" ", " "], "link": ["/docs/en/help/become.html"]}, {"title": [" Suggest a Site "], "desc": [" ", " "], "link": ["/docs/en/add.html"]}, {"title": [" Help "], "desc": [" ", " "], "link": ["/docs/en/help/helpmain.html"]}, {"title": [" Login "], "desc": [" ", " "], "link": ["/editors/"]}, {"title": [], "desc": [" ", " Share via Facebook "], "link": []}, {"title": [], "desc": [" ", " Share via Twitter "], "link": []}, {"title": [], "desc": [" ", " Share via LinkedIn "], "link": []}, {"title": [], "desc": [" ", " Share via e-Mail "], "link": []}, {"title": [], "desc": [" ", " "], "link": []}, {"title": [], "desc": [" ", " "], "link": []} ]
第一次运行scrapy的测试成功
接下来,咱们将使用GooSeeker API来实现网络爬虫,省掉对每一个item人工去生成和测试xpath的工做量。目前有2个计划:
在gsExtractor中封装一个方法:从xslt内容中自动提取每一个item的xpath
从gsExtractor的提取结果中自动提取每一个item的结果
具体选择哪一个方案,将在接下来的实验中肯定,并发布到gsExtractor新版本中。
2016-06-15:V1.0,首次发布