本文操作环境: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. CocoaPods常用终端命令及Profile文件简单介绍

    Pod常用终端命令 pod init 创建pod文件 pod install 开始安装第三方框架,如果网上有更新,会安装最新的 pod install --verbose--no-repo-updat ...

  2. MTK 常见的编译命令

    1: ./mk n(r) kernel; ./mk bootimage;当修改build-in 到kernel相关代码时,要使用此命令,具体文件参考如下: alps/kernel/ alps/medi ...

  3. iOS 单例模式 浅叙

    单例模式作用 可以保证在程序运行过程中,一个类只有一个实例,而且该实例易于供外界使用 从而方便地控制了实例个数,并节约系统资源 单例模式使用场合 在整个引用程序中,共享一份资源(这份资源只需要创建初始 ...

  4. Android 创建一个新的Activity

    本文转载自:http://www.cnblogs.com/wuyudong/p/5658020.html 新建一个项目,创建一个java类,继承自Activity 新建一个Android.xml文件: ...

  5. 转载:jsp中jstl标签的类似 if - else 语句 的语法

    原文链接:http://www.cnblogs.com/wanggd/archive/2013/05/27/3101788.html 在jsp中引入jstl的标签库和函数库 <%@ taglib ...

  6. org.apache packages can't be resolved in Eclipse解决方案

    删除.m2仓库里面对应的jar包,重新Maven->update project

  7. C# MVC模式 404 500页面设置方法

    <customErrors mode="On" defaultRedirect="Controllers/Action"> <error st ...

  8. Symantec Backup Exec备份作业服务器盘符变更

    Symantec Backup Exec的备份作业中,如果某个服务器的磁盘更改了盘符,如果不修改备份作业里面的相关配置,就会出现类似下面的错误信息,如下截图所示 因为这台服务器上我们将原先的G盘的盘符 ...

  9. c# UpdateLayeredWindow异形窗口

    #region UpdateLayeredWindow #region 重写窗体的 CreateParams 属性 protected override CreateParams CreatePara ...

  10. Activiti之 Exclusive Gateway

    一.Exclusive Gateway Exclusive Gateway(也称为XOR网关或更多技术基于数据的排他网关)经常用做决定流程的流转方向.当流程到达该网关的时候,所有的流出序列流到按照已定 ...