1)创建项目命令:

scrapy startproject tutorial

该命令将在当前目录下创建tutorial文件夹

2)定义Item

Items are containers that will be loaded with the scraped data;They are declared by creating a scrapy.Item class and defining its attibutes as scrapy.Field objects.

import scrapy

class DmozItem(scrapy.Item):
title=scrapy.Field()
link=scrapy.Field()
desc=scrapy.Field()

3)定义spider

To create a spider,you must subclass scrapy.Spider and define the three main mandatory attributes:

name:identifies the spiders

start_urls:a list of urls where the spider will begin to crawl from.

parse():a method of the spider,which will be called with the downloaded Response object of each start url.The response is passed to the method as the first and only argument.

The parse() methods is in charge of processing the response and returning scraped data(as Item object) and more urls to follow(as Request object).

import scrapy

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):
filename = response.url.split("/")[-2]
with open(filename, 'wb') as f:
f.write(response.body)

4)爬取

命令:scrapy crawl dmoz

5)storing the scraped data

命令:scrapy crawl dmoz -o items.json

that will generate a items.json file containing all scraped items,serialized in JSON

If you want to perform more complex things with the scraped items,you can write an Item Pipeline.

------------------------------------------------------------------------------------------------------------------------------------

Introduction to Selectors

There are several ways to extract data from web pages.Scrapy uses a mechanism based on XPath or CSS expressions called Scrapy Selectors.

You can see selectors as objects that represent nodes in the document structure.

Selectors have four basic methods:

xpath():returns a list of selectors

css():returns a list of selectors

extract():returns a unicode string with the selected data

re():returns a list of Unicode strings extracted by applying the regular expression given as argument.

Trying Selectors in the Shell:

Start a shell:

scrapy shell "http://www.dmoz.org/Computers/Programming/Languages/Python/Books/"

After the shell loads,you will have the response fetched in a local response variable,so if you type response.body() you will see the body of the response,or you can type response.headers to see its headers.

【scrapy】创建第一个项目的更多相关文章

  1. Django 创建第一个项目(转)

    转自(http://www.runoob.com/django/django-first-app.html) 前面写了不少python程序,由于之前都是作为工具用,所以命令行就足够了,最近写的测试用例 ...

  2. Angular安装及创建第一个项目

    Angular简介 AngularJS 诞生于2009年,由Misko Hevery 等人创建,后为Google所收购.是一款优秀的前端JS框架,已经被用于Google的多款产品当中.AngularJ ...

  3. python+Django创建第一个项目

    1.首先搭建好环境 1.1 安装pyhton,Linux系统中,python是系统自带的所以就不用安装 1.2 安装Django框架 使用pip安装: pip install django 1.3 检 ...

  4. 【3】Django创建第一个项目

    天地所以能长且久者,以其不自生,故能长生. --老子<道德经> 写在前面:Django在学习的过程中,我们会参考官方文档,从两部分进行讲解,第一部分主要是一个入门项目的搭建开发,第二部分是 ...

  5. 吴裕雄--天生自然Django框架开发笔记:Django 创建第一个项目

    Django 管理工具 安装 Django 之后,您现在应该已经有了可用的管理工具 django-admin.可以使用 django-admin 来创建一个项目: 可以来看下django-admin ...

  6. django创建第一个项目helloworld

    环境:centos 7,已安装python 3.6环境 1.安装django并创建django第一个项目 1.1.使用pip安装django# pip install Django或指定安装版本# p ...

  7. 用Maven创建第一个项目

    1.在Eclipse左侧的空白处点击鼠标右键,选择:New>Other : 2.选择Maven项目,点击"Next"按钮: 3.保持默认,直接点击“Next”按钮: 4.选择 ...

  8. cocos2d-x游戏开发(二)之创建第一个项目

    配置好开发环境之后,尝试创建一个cocos项目 (1)打开cocos2d-x安装目录,如D:\DIY\cocos2d-x-3.3 看到目录下有可执行文件 download-deps 以及 setup ...

  9. Cocos从入门到精通--《创建第一个项目:HelloWorld》

    上节课我们解说了cocos2-x v3.7版本号的下载安装,也展示了使用CocosStudio编译不同平台运行程序的方法,大家是不是对新版本号的Cocos引擎充满期待?今天我们就创建一个project ...

  10. 一、Spring Boot系列:通过Maven创建第一个项目

    1.打开idea选择创建工程 2.创建maven工程,同时选择jdk1.8 注意:不需要勾选其他选项 3.填写项目名称 4.创建好maven项目后,在pom.xml文件中导入Spring Boot需要 ...

随机推荐

  1. Mysql 访问远程数据库,报错:1130-host ... is not allowed to connect to this MySql server 开放mysql远程连接 不使用localhost

    参考:http://www.cnblogs.com/xyzdw/archive/2011/08/11/2135227.html 解决方法: 1. 改表法. 可能是你的帐号不允许从远程登陆,只能在loc ...

  2. Vickers Vane Pump - Hydraulic Vane Pump Failure: Cavitation, Mechanical Damage

    One of our readers recently wrote to me about the following questions: “Recently, we purchased a sec ...

  3. Oracle清空数据库中数据表数据的方法

    一.简介最近在项目发版测试的时候,导出dmp的时候不小心把开发库中的一些脏数据导出来了,测试那边导入进去之后一堆不规范的数据,为了不影响测试结果,于是总结了一个快速清空数据库数据表所有数据的方法. 二 ...

  4. spring踩坑

    org.springframework.web.util.NestedServletException: Request processing failed; nested exception is ...

  5. python UDP-数据报协议

    基于udp协议通信的套接字 服务端 from socket import * server = socket(AF_INET, SOCK_DGRAM) # SOCK_DGRAM=>数据报协议 s ...

  6. Docker客户端连接Docker Daemon的方式

    Docker为C/S架构,服务端为docker daemon,客户端为docker.service,支持本地unix socket域套接字通信与远程socket通信. 默认为本地unix socket ...

  7. (2) OpenSSL命令

    openssl命令的格式是"openssl command command-options args",command部分有很多种命令,这些命令需要依赖于openssl命令才能执行 ...

  8. 深入Linux内核架构——锁与进程间通信

    Linux作为多任务系统,当一个进程生成的数据传输到另一个进程时,或数据由多个进程共享时,或进程必须彼此等待时,或需要协调资源的使用时,应用程序必须彼此通信. 一.控制机制 1.竞态条件 几个进程在访 ...

  9. NFS共享存储服务部署

    第1章 NFS介绍 1.1 NFS基本概述 NFS(Network File System)网络文件系统 主要功能是通过局域网络让不同的主机系统之间可以共享文件或目录. NFS系统和Windows网络 ...

  10. C/C++函数指针详解(转)

    这两天在研究C/C++的函数指针,找到一篇讲解比较详细的博客,内容有点多,但是讲解得比较详细,适合初学者.特转之: 1.     无处不见的函数指针 使用函数指针可以设计出更优雅的程序,比如设计一个集 ...