1,引言

Scrapy的架构初探》一文讲解了Scrapy的架构,本文就实际来安装运行一下Scrapy爬虫。本文以官网的tutorial作为例子,完整的代码可以在github上下载。

2,运行环境配置

  • 本次测试的环境是:Windows10, Python3.4.3 32bit
  • 安装Scrapy :   $ pip install Scrapy                 #实际安装时,由于服务器状态的不稳定,出现好几次中途退出的情况

3,编写运行第一个Scrapy爬虫

3.1. 生成一个新项目:tutorial

$ scrapy startproject tutorial

项目目录结构如下:

3.2.  定义要抓取的item

# -*- 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. 定义Spider

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) 查错过程:查看官方的FAQstackoverflow上的信息,原来是scrapy在python3上测试还不充分,还有小问题。

3) 解决过程:
   A) 需要手工去下载twisted/internet下的 _win32stdio 和 _pollingfile,存放到python目录的lib\sitepackages\twisted\internet下
   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的测试成功

4,接下来的工作

接下来,我们将使用GooSeeker API来实现网络爬虫,省掉对每个item人工去生成和测试xpath的工作量。目前有2个计划:
  • 在gsExtractor中封装一个方法:从xslt内容中自动提取每个item的xpath
  • 从gsExtractor的提取结果中自动提取每个item的结果

具体选择哪个方案,将在接下来的实验中确定,并发布到gsExtractor新版本中

5,文档修改历史
 
2016-06-17:V1.0,首次发布

Scrapy:python3下的第一次运行测试的更多相关文章

  1. Python3下map函数的显示问题

    map函数是Python里面比较重要的函数,设计灵感来自于函数式编程.Python官方文档中是这样解释map函数的: map(function, iterable, ...) Return an it ...

  2. 论python3下“多态”与“继承”中坑

    1.背景: 近日切换到python3后,发现python3在多态处理上,有一些比较有意思的情况,特别记载,供大家参考... 以廖老师的python3教程中的animal 和dog的继承一节的代码做例子 ...

  3. python3下调用系统massagebox对话框

    #python3下调用系统massagebox对话框#先安装pwin32插件https://github.com/mhammond/pywin32/releases import win32apiim ...

  4. python3下获取主流浏览器和python的安装路径

    #coding=utf-8#python3下获取主流浏览器和python的安装路径#by dengpeiyou date:2018-07-09import winreg,os #取得浏览器的安装路径d ...

  5. scrapy windows下出现importError:No module named 'win32api'

    scrapy windows下出现importError:No module named 'win32api'需安装 pip install pypiwin32

  6. 在python3下使用OpenCV 显示图像

    在Python3下用使用OpenCV比在C,C++里开发不止快捷一点点, 原型开发的时候蛮有用. 这里用的OpenCV 加载图片, 用的imshow画图 # -*- coding: utf-8 -*- ...

  7. python3下django连接mysql数据库

    1.安装pymysql pip install pymysql 有一点需要注意,有的系统(比如ubuntu16.04)同时安装了python2和python3,而比较新的django需要在python ...

  8. python3下安装aiohttp遇到过的那些坑

    python3下安装aiohttp遇到过的那些坑 最近需要用到aiohttp这个库,在安装过程中遇到很多坑.google.baidu后,依然没有找到合适的解决方案. 后来通过去python官方的PyP ...

  9. 在python3下用PIL做图像处理

    Python Imaging Library (PIL)是python下的图像处理模块,支持多种格式,并提供强大的图形与图像处理功能. 目前PIL的官方最新版本为1.1.7,支持的版本为python ...

随机推荐

  1. python笔记之提取网页中的超链接

    python笔记之提取网页中的超链接 对于提取网页中的超链接,先把网页内容读取出来,然后用beautifulsoup来解析是比较方便的.但是我发现一个问题,如果直接提取a标签的href,就会包含jav ...

  2. Hibernate配置属性

    Hibernate配置属性 属性名 用途 hibernate.dialect 一个Hibernate Dialect类名允许Hibernate针对特定的关系数据库生成优化的SQL. 取值 full.c ...

  3. TreeSet与HashSet的区别

    Set是java中一个不包含重复元素的collection.更正式地说,set 不包含满足 e1.equals(e2) 的元素对 e1 和 e2,并且最多包含一个 null 元素.正如其名称所暗示的, ...

  4. js加载优化-二

    http://www.cnblogs.com/radom/archive/2011/04/26/2028886.html ontrolJS 主要为了是解决网页加载中Js文件的性能问题,ControlJ ...

  5. 【转】 怎么刷入BOOT.IMG(刷机后开机卡在第一屏的童鞋请注意)-------不错不错

    原文网址:http://bbs.gfan.com/android-3440837-1-1.html 之前呢,有好多机油问我关于刷机卡屏的问题,我解答了好多,但一一解答太费事了,在这里给大家发个贴吧.其 ...

  6. Letter Combinations of a Phone Number 解答

    Question Given a digit string, return all possible letter combinations that the number could represe ...

  7. Merge k Sorted Lists 解答

    Question Merge k sorted linked lists and return it as one sorted list. Analyze and describe its comp ...

  8. Light OJ 1067 Combinations (乘法逆元)

    Description Given n different objects, you want to take k of them. How many ways to can do it? For e ...

  9. PHP流程控制中不经常使用的替代语法

    准备做个wordpress的主题.结果看到了例如以下的语法: <div id="primary" class="content-area"> < ...

  10. javascript高级知识点——memoization

    memoization是一种非常有用的优化技术,它缓存特定输入产生的相应结果.这样麻烦的查找和迭代计算可以尽可能的减少. 它基本的思想是针对特定的输入,已经计算过的结果都是通过缓存当中的数据直接返回而 ...