scrapy系统学习(1)--概要
本文操作环境:ubuntu14.04
一.安装Scrapy/Mysql/MySQLdb
- 参照官网教程安装Scrapy
#sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 627220E7
#echo 'deb http://archive.scrapy.org/ubuntu scrapy main' | sudo tee /etc/apt/sources.list.d/scrapy.list
#sudo apt-get update && sudo apt-get install scrapy-0.25
- 安装Mysql
#apt-get install mysql-server
- 安装第三方库MySQLdb
#apt-get install python2.7-mysqldb
二.爬虫的目标是抓取
start一个爬虫项目,目标是抓取http://www.mininova.org/today 界面上每日更新的内容并以json格式保存。
root@alexknight:/home# scrapy startproject mininova mininova/
├── mininova
│ ├── __init__.py
│ ├── items.py
│ ├── pipelines.py
│ ├── settings.py
│ └── spiders
│ └── __init__.py
└── scrapy.cfg
items一般与pipelines.py一起使用,items可以定义自己需要抓取的格式,pipelines.py可以定义抓取内容。
items.py:
# -*- coding: utf-8 -*- # Define here the models for your scraped items
#
# See documentation in:
# http://doc.scrapy.org/en/latest/topics/items.html import scrapy class MininovaItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
url=scrapy.Field()
name=scrapy.Field()
description=scrapy.Field()
size=scrapy.Field()
在spider新建文件mininova_spider.py:
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors import LinkExtractor
from mininova.items import MininovaItem
class MininovaSpider(CrawlSpider):
name='mininova'
allowed_domains=['mininova.org']
start_urls = ['http://www.mininova.org/today']
rules = [Rule(LinkExtractor(allow=['/tor/\d+']), 'parse_torrent')] def parse_torrent(self, response):
#global MininovaItem
torrent = MininovaItem()
torrent['url'] = response.url
torrent['name'] = response.xpath("//h1/text()").extract()
torrent['description'] = response.xpath("//div[@id='description']").extract()
torrent['size'] = response.xpath("//div[@id='info-left']/p[2]/text()[2]").extract()
return torrent
将数据json化
root@alexknight:/home/mininova#scrapy crawl mininova -o scraped_data.json
root@alexknight:/home/mininova#vim scraped_data.json
[{"url": "http://www.mininova.org/tor/13278887", "size": [], "description": ["<div id=\"description\">\n\n<iframe id=\"share-facebook\" src=\"http://www.facebook.com/plugins/like.php?href=http://www.mininova.org/tor/13278887&layout=box_count&show_faces=false&width=50&action=like&colorscheme=light&height=65\" scrolling=\"no\" frameborder=\"0\"></iframe>\n\nYoung Lucid's debut release features Joell Oritz, one quarter of the Shady Records\u2019 super group, Slaughterhouse. The emotional production from 2 Deep & MeccaGodZilla set the tone for the lyrical duo as they trade bars about a world they long to escape. \u201cRunaway\u201d is a mere preview of Young Lucid\u2019s upcoming project, \u201cLucid Dreams\u201d which is slated for a 2015 release.<br>\n<br>\n<a target=\"_blank\" rel=\"nofollow\" href=\"http://www.younglucid.com/\">www.younglucid.com</a><br>\n<a target=\"_blank\" rel=\"nofollow\" href=\"http://www.meccagodzilla.com\">www.meccagodzilla.com</a><br>\n<br>\n<div class=\"clear-left\"></div>\n</div>"], "name": ["NEW: Runaway Ft. Joell Ortiz by Young Lucid "]}]
scrapy系统学习(1)--概要的更多相关文章
- python应用:爬虫框架Scrapy系统学习第四篇——scrapy爬取笔趣阁小说
使用cmd创建一个scrapy项目: scrapy startproject project_name (project_name 必须以字母开头,只能包含字母.数字以及下划线<undersco ...
- python应用:爬虫框架Scrapy系统学习第三篇——初识scrapy
scrapy的最通用的爬虫流程:UR2IM U:URL R2:Request 以及 Response I:Item M:More URL 在scrapy shell中打开服务器一个网页 cmd中执行: ...
- python应用:爬虫框架Scrapy系统学习第二篇——windows下安装scrapy
windows下安装scrapy 依次执行下列操作: pip install wheel pip install lxml pip install PyOpenssl 安装Microsoft visu ...
- python应用:爬虫框架Scrapy系统学习第一篇——xpath详解
HTML的三大概念:标签.元素以及属性 标签:尖括号中的文本 例:<head>……</head> 标签通常成对出现 元素:标签中的所有内容 元素中可包 ...
- scrapy爬虫学习系列一:scrapy爬虫环境的准备
系列文章列表: scrapy爬虫学习系列一:scrapy爬虫环境的准备: http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_python_00 ...
- 零基础如何系统学习Java Web
零基础如何系统学习Java Web? 我来给你说一说 你要下决心,我要转行做开发,这样你才能学成. 你要会打字,我公司原来有一个程序员,打字都是两个手一指禅,身为程序员你一指禅怎么写出的代码,半个 ...
- Unity3D 装备系统学习Inventory Pro 2.1.2 基础篇
前言 前一篇 Unity3D 装备系统学习Inventory Pro 2.1.2 总结 基本泛泛的对于Inventory Pro 这个插件进行了讲解,主要是想提炼下通用装备系统结构和类体系.前两天又读 ...
- MES系统学习
MES系统是当今制造型企业信息化的热点,而统一建模语言UML是面向对象建模的标准语言,在软件工程发挥着重要作用.MES系统如何进行UML建模呢,今天和大家重点讨论一下MES系统的UML建模方法,请看本 ...
- 001 今天开始系统学习C#
2016-01-16 之前只是大概了解过c#语言,感觉掌握不牢靠.现在开始系统学习C#.现以该博客作为学习笔记,方便后续查看.C# 目标:系统掌握c#知识 时间:30天 范围:C#基础,Winform ...
随机推荐
- java 实现 LINQ 的一些框架记录一下
jOOQ: http://www.jooq.org JINQ: http://www.jinq.org JaQue: http://github.com/TrigerSoft/jaque JaQu: ...
- 私有Pods封装个推SDK功能(解决方案)
一:运用场景 公司中同时有好几个APP在开发,而且每个APP都有使用到集成个推SDK来处理消息的功能,以前的做法是每个APP都去集成并在AppDelegate处理一些SDK的代码,包含个推基础配置.消 ...
- 【读书笔记】iOS网络-使用Game Kit实现设备间通信
Apple的Game Kit框架可以实现没有网络状况下的设备与设备之间的通信,这包括没有蜂窝服务,无法访问Wi-Fi基础设施以及无法访问局域网或Internet等情况.比如在丛林深处,高速公路上或是建 ...
- 使用PL/SQL编写存储过程访问数据库
一.实验目的 熟悉使用存储过程来进行数据库应用程序的设计. 二.实验内容 对学生-课程数据库,编写存储过程,完成下面功能: 1.统计离散数学的成绩分布情况,即按照各分数段统计人数: 2.统计任意一门课 ...
- 转载 sql 存储过程与函数区别
SQL Server用户自定义函数和存储过程有类似的功能,都可以创建捆绑SQL语句,存储在server中供以后使用.这样能够极大地提高工作效率,通过以下的各种做法可以减少编程所需的时间: 重复使用编程 ...
- MySQL 系列(五) 多实例、高可用生产环境实战
MySQL 系列(五) 多实例.高可用生产环境实战 第一篇:MySQL 系列(一) 生产标准线上环境安装配置案例及棘手问题解决 第二篇:MySQL 系列(二) 史上最屌.你不知道的数据库操作 第三 ...
- html5 如何进行自定义属性的定义和查询
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>T ...
- Tomcat中取消断点
启动tomcat时,myeclipse报错: This kind of launch is configured to openthe debug perspective when it suspen ...
- iOS 读取相册二维码,兼容ios7(使用CIDetector 和 ZXingObjC)
ios从相册读取二维码,在ios8以上,苹果提供了自带的识别图片二维码的功能,这种方式效率最好,也是最推荐的,但是如果你的系统需要向下兼容ios7,就必须用其他方式. 这里我选择的是 ZXingObj ...
- Java 注释说明
注释 什么是注释呢?就是标注解释的意思,主要用来对Java代码进行说明.Java中有三种注释方式 (1):// :注释单行语句 示例: //定义一个值为10的int变量 int a = 10; (2) ...