本文操作环境: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&amp;layout=box_count&amp;show_faces=false&amp;width=50&amp;action=like&amp;colorscheme=light&amp;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 &amp; 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)--概要的更多相关文章

  1. python应用:爬虫框架Scrapy系统学习第四篇——scrapy爬取笔趣阁小说

    使用cmd创建一个scrapy项目: scrapy startproject project_name (project_name 必须以字母开头,只能包含字母.数字以及下划线<undersco ...

  2. python应用:爬虫框架Scrapy系统学习第三篇——初识scrapy

    scrapy的最通用的爬虫流程:UR2IM U:URL R2:Request 以及 Response I:Item M:More URL 在scrapy shell中打开服务器一个网页 cmd中执行: ...

  3. python应用:爬虫框架Scrapy系统学习第二篇——windows下安装scrapy

    windows下安装scrapy 依次执行下列操作: pip install wheel pip install lxml pip install PyOpenssl 安装Microsoft visu ...

  4. python应用:爬虫框架Scrapy系统学习第一篇——xpath详解

    HTML的三大概念:标签.元素以及属性 标签:尖括号中的文本       例:<head>……</head> 标签通常成对出现 元素:标签中的所有内容        元素中可包 ...

  5. scrapy爬虫学习系列一:scrapy爬虫环境的准备

    系列文章列表: scrapy爬虫学习系列一:scrapy爬虫环境的准备:      http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_python_00 ...

  6. 零基础如何系统学习Java Web

    零基础如何系统学习Java Web?   我来给你说一说 你要下决心,我要转行做开发,这样你才能学成. 你要会打字,我公司原来有一个程序员,打字都是两个手一指禅,身为程序员你一指禅怎么写出的代码,半个 ...

  7. Unity3D 装备系统学习Inventory Pro 2.1.2 基础篇

    前言 前一篇 Unity3D 装备系统学习Inventory Pro 2.1.2 总结 基本泛泛的对于Inventory Pro 这个插件进行了讲解,主要是想提炼下通用装备系统结构和类体系.前两天又读 ...

  8. MES系统学习

    MES系统是当今制造型企业信息化的热点,而统一建模语言UML是面向对象建模的标准语言,在软件工程发挥着重要作用.MES系统如何进行UML建模呢,今天和大家重点讨论一下MES系统的UML建模方法,请看本 ...

  9. 001 今天开始系统学习C#

    2016-01-16 之前只是大概了解过c#语言,感觉掌握不牢靠.现在开始系统学习C#.现以该博客作为学习笔记,方便后续查看.C# 目标:系统掌握c#知识 时间:30天 范围:C#基础,Winform ...

随机推荐

  1. UITableView

    知识点: 1)UITableView 2)UITableViewCell ====================================================== 一.UITabl ...

  2. Android 数据库SQLite 写入SD卡

    如果手机没有root,数据库文件是无法查看到的,不方便调试. 最好的办法是把数据库写进SD卡. 修改的地方有两处: 1.在你的helper类中把数据库文件名称 DATABASE_NAME 由原来的一个 ...

  3. 从DACPAC文件中读取元数据

    SQL数据库项目生成时会生成dacpac文件,可从中读出所需的元数据,进行一些转换(如生成数据字典)   var model = new TSqlModel(@"D:\kljob\CardL ...

  4. 原 ng-include用法分析以及多标签页面的简单实现方式

    Demo:http://webenh.chinacloudsites.cn/Default/Demo2 在平时的项目开发中,应该会经常遇到上图所示的需求,就是在一个页面中有多个标签,被选中的标签颜色会 ...

  5. .htaccess绑定网站主目录的方法

    用过cpanel的基本都会知道要把自己的服务器脚本传到public_htmk这个文件夹下,这个就是整个网站的主目录,普通的Apache服务器可以用下面的代码来实现这种效果 RewriteEngine ...

  6. Java Se:自定义ClassLoader

    JVM是如何知道java.lang包中的类的?JVM又是如何知道我们应用中的类的?我们的应用中明明是有某个类, 但是JVM却抛出ClassNotFoundException,这是为什么?XxxImpl ...

  7. 遍历map的常用方法

    Map< String, String> map = new HashMap<String, String>();  map.put("a", " ...

  8. Sql server之sql注入篇

    SQL Injection 关于sql注入的危害在这里就不多做介绍了,相信大家也知道其中的厉害关系.这里有一些sql注入的事件大家感兴趣可以看一下 防范sql注入的方法无非有以下几种: 1.使用类型安 ...

  9. 无法连接到WMI提供程序 三种解决办法

    无法连接到WMI 提供程序 请注意,你只能使用SQL Server 配置管理器来管理SQL Server 2005服务器.找不到指定的模块.[0x8007007e] 解决方案1: 检查一下 windo ...

  10. MaskedTextBox的聚焦和光标位置

    遇到一个关于MaskedTextBox聚焦和光标居左的问题. ①. 先说说聚焦: 起初给MaskedTextBox设置了Focus(),运行之后,并没有作用. 然后又给Form添加了Load事件,又设 ...