接触了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. python中的redis定义

    redis官方介绍,python的redis客户端可以一处定义,处处使用.到底是不是这样呢?是不是只要在配置文件里定义一次,不管在哪都可以使用了? 下面用代码说明: import redis impo ...

  2. A + B Problem,hdu-1000

    A + B Problem Problem Description Calculate A + B.   Input Each line will contain two integers A and ...

  3. storyboard和xib的区别

    storyboard只是算是帮你布局,流程什么的,xib的另一种形势,比xib功能多,但是和分享完全没有半点关系 你暂时可以理解为高级xib

  4. JAVA三大特性之多态

    面向对象的三大特性:封装.继承.多态.从一定角度来看,封装和继承几乎都是为多态而准备的.这是我们最后一个概念,也是最重要的知识点. 多态的定义:指允许不同类的对象对同一消息做出响应.即同一消息可以根据 ...

  5. matlab的绘图保存

      matlab的绘图和可视化能力是不用多说的,可以说在业内是家喻户晓的.Matlab提供了丰富的绘图函数,比如ez**系类的简易绘图函数,surf.mesh系类的数值绘图函数等几十个.另外其他专业工 ...

  6. selenium太有爱,我已离不开!!!

    自动化测试,超有用. PROXY,PLUGIN,PROFILE,WINDOWS HANDLE个个搞定!!! from selenium import webdriver from selenium.c ...

  7. Keil中LIB库的作用、生成与调用

    LIB库有什么用,一个简单的例子就是Silicon Labs为C8051F单片机USB提供的USBXpress LIB库了,如USB发送数据.接收数据等,都是通用性很强的函数,但因为保密的原因,这个函 ...

  8. 脱机BT transmission

    脱机BT transmission http://192.168.1.1:9091 也可以在使用Transmission Remote软件进行管理,如果下载太慢检查QoS 如果开了防火墙,需要打开默认 ...

  9. 在线服装零售商Betabrand获得650万美元风投 - 投资风向 - 创业邦

    在线服装零售商Betabrand获得650万美元风投 - 投资风向 - 创业邦 在线服装零售商Betabrand获得650万美元风投

  10. ext3中xtype属性汇总

    基本组件: xtype Class 描述 button Ext.Button 按钮 splitbutton Ext.SplitButton 带下拉菜单的按钮 cycle Ext.CycleButton ...