【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需要 ...
随机推荐
- 聊天室(C++客户端+Pyhton服务器)3.群功能添加
创建群 数据库 group_table(user, name) grpuser_table(grpname,user) 按下添加群按钮 // 创建群组void CUserDialog::OnBnCli ...
- MFC限制窗口大小
MFC限制窗口大小 使用:WM_GETMINMAXINFO message void OnGetMinMaxInfo(MINMAXINFO* lpMMI) { lpMMI->ptMinTrack ...
- 【2019.6.2】python:json操作、函数、集合、random()等
一.json操作: json就是一个字符串,从文件中读取json,必须是json格式.j'son串中必须是双引号,不能有单引号,单引号不能转换 1.1使用: import json #使用json先引 ...
- EBS ORACLE采购对账单自动产生发票
只要传入个对账单号,然后跑数据抛到接口表,运行接口请求,就可以自动生成发票 create or replace package body pkg_ap_check_by_po is --创建ap发票 ...
- Ubuntu配置NFS服务器
NFS(Network File System)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TCP/IP网络共享资源.在NFS的应用中,本地NFS的客户端应用可 ...
- 让自己习惯C++
条款1.C++是一个语言联邦 过程形式(procedural) 面向对象形式(object-oriented) 函数形式(function) 泛型形式(generic) 元编程形式(metaprogr ...
- 搭建Mysql主从复制
mysql 主从复制流程图 Server version: 10.0.24-MariaDB-7 Ubuntu 16.04 Master 记录二进制文件 导出数据并记录二进制位置 导入数据,设置二进制位 ...
- tornado框架基础02-输入和输出
01 输出 write bytes类型 class IndexHandler(tornado.web.RequestHandler): def get(self): self.write(b'Torn ...
- Could not resolve dependencies for project com.shadow:shlang:jar:1.0-SNAPSHOT:
maven打包项目出现缺少jar包错误 如果是将本地引用的jar包放在了lib目录下并通过下面方式引入 解决方案为 <dependency> <groupId>com.o ...
- 如何将数据放入下拉框List值
最近在做下拉框,里面放入值大概有这几种 //仓库业务类型 第一种 model.addAttribute("warehouseBizTypeList", basePropertySe ...