【scrapy】创建第一个项目
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】创建第一个项目的更多相关文章
- Django 创建第一个项目(转)
转自(http://www.runoob.com/django/django-first-app.html) 前面写了不少python程序,由于之前都是作为工具用,所以命令行就足够了,最近写的测试用例 ...
- Angular安装及创建第一个项目
Angular简介 AngularJS 诞生于2009年,由Misko Hevery 等人创建,后为Google所收购.是一款优秀的前端JS框架,已经被用于Google的多款产品当中.AngularJ ...
- python+Django创建第一个项目
1.首先搭建好环境 1.1 安装pyhton,Linux系统中,python是系统自带的所以就不用安装 1.2 安装Django框架 使用pip安装: pip install django 1.3 检 ...
- 【3】Django创建第一个项目
天地所以能长且久者,以其不自生,故能长生. --老子<道德经> 写在前面:Django在学习的过程中,我们会参考官方文档,从两部分进行讲解,第一部分主要是一个入门项目的搭建开发,第二部分是 ...
- 吴裕雄--天生自然Django框架开发笔记:Django 创建第一个项目
Django 管理工具 安装 Django 之后,您现在应该已经有了可用的管理工具 django-admin.可以使用 django-admin 来创建一个项目: 可以来看下django-admin ...
- django创建第一个项目helloworld
环境:centos 7,已安装python 3.6环境 1.安装django并创建django第一个项目 1.1.使用pip安装django# pip install Django或指定安装版本# p ...
- 用Maven创建第一个项目
1.在Eclipse左侧的空白处点击鼠标右键,选择:New>Other : 2.选择Maven项目,点击"Next"按钮: 3.保持默认,直接点击“Next”按钮: 4.选择 ...
- cocos2d-x游戏开发(二)之创建第一个项目
配置好开发环境之后,尝试创建一个cocos项目 (1)打开cocos2d-x安装目录,如D:\DIY\cocos2d-x-3.3 看到目录下有可执行文件 download-deps 以及 setup ...
- Cocos从入门到精通--《创建第一个项目:HelloWorld》
上节课我们解说了cocos2-x v3.7版本号的下载安装,也展示了使用CocosStudio编译不同平台运行程序的方法,大家是不是对新版本号的Cocos引擎充满期待?今天我们就创建一个project ...
- 一、Spring Boot系列:通过Maven创建第一个项目
1.打开idea选择创建工程 2.创建maven工程,同时选择jdk1.8 注意:不需要勾选其他选项 3.填写项目名称 4.创建好maven项目后,在pom.xml文件中导入Spring Boot需要 ...
随机推荐
- free - 显示系统中已用和未用的内存空间总和.
总览 (SYNOPSIS) free [-b | -k | -m] [-o] [-s delay ] [-t] [-V] 描述 (DESCRIPTION) free 显示 系统中 已用和未用的 物理内 ...
- docker的安装及基础操作与镜像构建
仓库配置及安装启动 [root@localhost ~]# yum install -y yum-utils device-mapper-persistent-data lvm2 [root@loca ...
- VC无窗口控制台程序
VC无窗口控制台程序 #pragma comment(linker,"/subsystem:\"Windows\" /entry:\"mainCRTStartu ...
- Python Syntax Summary
# _*_ coding: utf-8 _*_ """########################################################## ...
- Sybase PowerDesigner 16.5注册码
Sybase PowerDesigner16.5安装之后一段时间就会过期,破解办法是:将pdflm16.dll文件覆盖原来的即可. pdflm16.dll文件下载地址:链接:https://pan.b ...
- web前端中的一些注释表达法
1.HTML注释 <!--注释的内容--> 注释的地方(根据个人习惯可能有所不同): 结束标签的后面,这一切都是为了程序在嵌套的时候更加方便.明了,如: <div class=&qu ...
- 函数内部属性之arguments和this
在函数内部,有两个特殊的对象:arguments和this. 1.arguments arguments是一个类数组对象.包含着传入函数中的所有参数.但这个对象还有一个名叫callee的属性,该属性是 ...
- 牛客网练习赛25 C 再编号
链接:https://www.nowcoder.com/acm/contest/158/C来源:牛客网 定义对 a 的再编号为 a' ,满足 . 现在有 m 次询问,每次给定 x,t ,表示询问经过 ...
- 复制webp图片到word || 微信webp图片不能复制 || 如何复制webp到word
用IE浏览器打开Alt+A 复制直接去word粘贴, 用IE浏览器打开Alt+A 复制直接去word粘贴 用IE浏览器打开Alt+A 复制直接去word粘贴 用IE浏览器打开Alt+A 复制直接去wo ...
- 二叉树的创建一数据结构一C++
#include <iostream> using namespace std; //二叉树结点typedef struct BitNode { cha ...