接触了scrapy ,发现爬虫效率高了许多,借鉴大神们的文章,做了一个爬虫练练手:

我的环境是:Ubuntu14.04 + python 2.7 + scrapy 0.24

目标 topit.me

一、创建project

 scrapy startproject topit

二、定义Item

import scrapy

class TopitItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
url = scrapy.Field()

三、在spider 文件夹中创建 topit_spider.py

# -*- coding: utf-8 -*-

#!/usr/bin/env python
#File name :topit_spider.py
#Author:Mellcap from scrapy.contrib.spiders import CrawlSpider,Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from topit.items import TopitItem
import re
from scrapy.http import Request
from scrapy.selector import Selector class TopitSpider(CrawlSpider):
name = "topit"
allowed_domains = ["topit.me"]
start_urls=["http://www.topit.me/"]
rules = (Rule(SgmlLinkExtractor(allow=('/item/\d*')), callback = 'parse_img', follow=True),)
def parse_img(self, response):
urlItem = TopitItem()
sel = Selector(response)
for divs in sel.xpath('//a[@rel="lightbox"]'):
img_url=divs.xpath('.//img/@src').extract()[0]
urlItem['url'] = img_url
yield urlItem

四、定义pipelines

# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html from topit.items import TopitItem class TopitPipeline(object):
def __init__(self):
self.mfile = open('test_topit.html', 'w')
def process_item(self, item, spider):
text = '<img src="' + item['url'] + '" alt = "" />'
self.mfile.writelines(text)
def close_spider(self, spider):
self.mfile.close()

五、设置一下 setting.py

在后面加入一行:

ITEM_PIPELINES={'topit.pipelines.TopitPipeline': 1,} 

保存后就大功告成了/

接着打开终端

运行:

cd topit
scrapy crawl topit

然后会在topit文件夹中发现test_topit 文件

打开之后在浏览器就可以看到图片了

接下来传到Github上:

爬虫已经做好了,在远程建立一个空库

一、

二、在本地建立版本库

theone@Mellcap:~$ cd topit
theone@Mellcap:~/topit$ git init
初始化空的 Git 版本库于 /home/theone/topit/.git/
theone@Mellcap:~/topit$ git status
位于分支 master 初始提交 未跟踪的文件:
(使用 "git add <file>..." 以包含要提交的内容) scrapy.cfg
test_topit.html
topit/ 提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
theone@Mellcap:~/topit$ git add scrapy.cfg
theone@Mellcap:~/topit$ git add topit/
theone@Mellcap:~/topit$ git commit -m'scrapy_topit'

三、跟远程库建立连接

theone@Mellcap:~/topit$ git remote add origin git@github.com:Mellcap/scrapy_topit.git
theone@Mellcap:~/topit$ git push -u origin master

四、完成

在github上看到了自己的爬虫了。

小试牛刀——爬topit.me的图片,附github简易上传教程的更多相关文章

  1. github代码上传教程

    github 上传代码步骤 一.git以及Github Git是个正快速成长的版本控制系统,它由GitHub维护. 优势: 1.支持离线开发,离线Repository. 2.强大的分支功能,适合多个独 ...

  2. 【Android实战】----基于Retrofit实现多图片/文件、图文上传

    本文代码详见:https://github.com/honghailiang/RetrofitUpLoadImage 一.再次膜拜下Retrofit Retrofit不管从性能还是使用方便性上都非常屌 ...

  3. 使用html5 FileReader获取图片,并异步上传到服务器(不使用iframe)

    使用html5 FileReader获取图片,并异步上传到服务器(不使用iframe)   原理: 1.使用FileReader 读取图片的base64编码 2.使用ajax,把图片的base64编码 ...

  4. #添加图片,最多只能上传9张.md

    #添加图片,最多只能上传9张.md 前端页面: ```javascript <form id="imgForm" enctype="multipart/form-d ...

  5. 适应各浏览器图片裁剪无刷新上传jQuery插件(转)

    看到一篇兼容性很强的图片无刷新裁剪上传的帖子,感觉很棒.分享下!~ 废话不多说,上效果图. 一.首先建立如下的一个page <!DOCTYPE html> <html xmlns=& ...

  6. puzz: 图片和表单上传的不一致问题

    1.    方向1 用户提交表单, 图片和表单同步上传.(由同一服务器处理, 服务器压力大. 没有分离) 2.    方向2 图片和表单分开上传. 如图片访问ftp,表单提交后台(图片和后台分离) 2 ...

  7. [IDEA_3] IDEA 配置 GitHub 并上传项目

    0. 说明 参考 Git & GitHub 的安装配置 IDEA 配置 GitHub 并上传项目 1. 安装配置 Git & GitHub 参照 Git & GitHub 的安 ...

  8. 本地项目通过github客户端上传到github网站上

    一.github客户端上传步骤 一.github客户端上传步骤1.百度搜索关键字: GitHub 离线安装包 ==> Github_3.3.4.0版本链接:https://pan.baidu.c ...

  9. github客户端上传代码

    在window下安装github客户端上传代码 第一步:创建Github新账户 第二步:新建仓库 第三步:安装Github shell程序,地址:http://windows.github.com/ ...

随机推荐

  1. 转:AM335x启动流程(BootRom->MLO->Uboot)

    http://blog.chinaunix.net/uid-28458801-id-3486399.html 参考文件: 1,AM335x ARM Cortex-A8 Microprocessors ...

  2. cf D. Dima and Trap Graph

    http://codeforces.com/contest/366/problem/D 遍历下界,然后用二分求上界,然后用dfs去判断是否可以. #include <cstdio> #in ...

  3. 自制单片机之四……LCD1602的驱动

    LCD1602已很普遍了,具体介绍我就不多说了,市面上字符液晶绝大多数是基于HD44780液晶芯片的,控制原理是完全相同的,因此HD44780写的控制程序可以很方便地应用于市面上大部分的字符型液晶.字 ...

  4. AsyncCTP &IdentityModel

    AsyncCTP.0.3.nupkg http://pan.baidu.com/s/1kVhPaYj Microsoft.IdentityModel.6.1.7600.16394  http://pa ...

  5. Linux & Python 导航目录

    < Python学习手册(第4版)>< Python Cookbook(第2版)>中文版.pdf< Python 高级编程>< Python 基础教程 第二版 ...

  6. js全局函数

    http://www.w3cschool.cc/jsref/jsref-obj-global.html 以前没搞懂JS的全局函数,全局函数和window对象的函数不一样.全局函数不属于任何一个内置对象 ...

  7. 虚拟ip配置

    虚 拟IP地址(VIP) 是一个不与特定计算机或一个计算机中的网络接口卡(NIC)相连的IP地址.数据包被发送到这个VIP地址,但是所有的数据还是经过真实的网络接口. VIPs大部分用于连接冗余:一个 ...

  8. n%i之和

    题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1168 题意:给定一个n,注意这里n小于10^12,求 分析:早些时 ...

  9. c指针点滴4-指针的值

    #include <stdio.h> #include <stdlib.h> void main() { ; int *p = &num; //double *p1 = ...

  10. [Cycle.js] Read effects from the DOM: click events

    So far we only had effects that write something to the external world, we are not yet reading anythi ...