【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需要 ...
随机推荐
- 通过JS加载XML文件,跨浏览器兼容
引言 通过JS加载XML文件,跨多种浏览器兼容. 在Chrome中,没有load方法,需要特殊处理! 解决方案 部分代码 try //Internet Explorer { xmlDoc=new Ac ...
- iview table icon dorpdown html页面级别vue组件 #vuez#
iview table icon dorpdown html页面级别vue组件 <!DOCTYPE html> <html> <head> <meta cha ...
- 一条update语句优化小记
遇到性能问题的sql如下: sql1: UPDATE amlclientlevel a SET a.client_value = (SELECT l.client_value ...
- python基础:函数传参、全局变量、局部变量、内置函数、匿名函数、递归、os模块、time模块
---恢复内容开始--- 一.函数相关: 1.1位置参数: ef hello(name,sex,county='china'): pass #hello('hh','nv') #位置参数.默认参数 1 ...
- List<T>排序
List<Student> studentList = new List<Student>(); Student s = new Student(); s.Name = &qu ...
- ie8兼容性
ie8下不支持css的nth-child()样式解决方法一:使用jQuery的nth-child()方法例:$(".ability-head-list ul li:nth-child(1) ...
- luogu 1113 杂务--啥?最长路?抱歉,我不会
P1113 杂务 题目描述 John的农场在给奶牛挤奶前有很多杂务要完成,每一项杂务都需要一定的时间来完成它.比如:他们要将奶牛集合起来,将他们赶进牛棚,为奶牛清洗乳房以及一些其它工作.尽早将所有杂务 ...
- MySQL字符集设定与查询
一.字符集设定 MySQL数据库允许对服务器.数据库.数据表.数据列级别的字符集作出互不影响的设定. 1.对服务器字符集设定是通过配置文件中选项character-set-server 设置,如 ch ...
- DB2表空间
https://www.ibm.com/developerworks/cn/data/library/techarticles/dm-0902yuancg/ 临时表空间的使用 (sorts or jo ...
- memcached内存分配
Memcached默认情况下采用了名为Slab Allocator的机制分配.管理内存,最大单个存储对象大小为1M. page:分配给slab的最小内存空间,默认为1M,可以在启动时通过-l参数修改 ...