【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需要 ...
随机推荐
- Metinfo 5.3.19管理员密码重置漏洞复现
Metinfo 5.3.19管理员密码重置漏洞 操作系统:Windows 10专业版 kali linux 网站环境:UPUPW 5.3 使用工具:burpsuite 1.7 beta 漏洞分 ...
- CAD交互绘制虚线(网页版)
用户可以在CAD控件视区任意位置绘制直线. 主要用到函数说明: _DMxDrawX::DrawLine 绘制一个直线.详细说明如下: 参数 说明 DOUBLE dX1 直线的开始点x坐标 DOUBLE ...
- resnet.caffemodel
http://blog.csdn.net/baidu_24281959/article/details/53203757
- Java笔记:编写第一个Java程序
2017.6.17 1.编写第一个Java程序 创建text文本,命名第一个Java程序.txt 在里面编写Java代码 public class Demo1{ public static void ...
- luogu 1608 路径统计--最短路计数
https://www.luogu.org/problemnew/show/P1608 题意https://www.cnblogs.com/rmy020718/p/9440588.html相似,建议还 ...
- MySQL(C#的链接姿势)
介绍 这篇随笔主要介绍MySQL的基础API的使用姿势 基本使用姿势: 第一步:登陆数据库 string connStr = "Database=start;datasource=127.0 ...
- 3. 对系统表空间使用Raw磁盘分区
3. 对系统表空间使用Raw磁盘分区 可以将raw磁盘分区用作InnoDB系统表空间中的数据文件.此技术可在Windows和某些Linux和Unix系统上启用非缓冲I/O,而无需文件系统开销.使用和不 ...
- Linux网络技术管理
1. OSI七层模型和TCP/IP四层模型 1.1 osi 七层模型 Open System interconnection,开放系统互连参考模型是国际标准化组织(ISO)制定的一个用于计算机或通信系 ...
- JavaScript中的内置函数
JavaScript中的内置函数 制作人:全心全意 在使用JavaScript语言时,除了可以自定义函数之外,还可以使用JavaScript的内置函数,这些内置函数是由JavaScript语言自身提供 ...
- 老男孩老师的博客地址 - 转自devops1992
害怕他那天不让人看了,所以我就复制一份到我自己的博客里. http://www.bootcdn.cn/bootstrap/ bootstrap cdn在线地址 http://www.cnblogs. ...