小试牛刀——爬topit.me的图片,附github简易上传教程
接触了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简易上传教程的更多相关文章
- github代码上传教程
github 上传代码步骤 一.git以及Github Git是个正快速成长的版本控制系统,它由GitHub维护. 优势: 1.支持离线开发,离线Repository. 2.强大的分支功能,适合多个独 ...
- 【Android实战】----基于Retrofit实现多图片/文件、图文上传
本文代码详见:https://github.com/honghailiang/RetrofitUpLoadImage 一.再次膜拜下Retrofit Retrofit不管从性能还是使用方便性上都非常屌 ...
- 使用html5 FileReader获取图片,并异步上传到服务器(不使用iframe)
使用html5 FileReader获取图片,并异步上传到服务器(不使用iframe) 原理: 1.使用FileReader 读取图片的base64编码 2.使用ajax,把图片的base64编码 ...
- #添加图片,最多只能上传9张.md
#添加图片,最多只能上传9张.md 前端页面: ```javascript <form id="imgForm" enctype="multipart/form-d ...
- 适应各浏览器图片裁剪无刷新上传jQuery插件(转)
看到一篇兼容性很强的图片无刷新裁剪上传的帖子,感觉很棒.分享下!~ 废话不多说,上效果图. 一.首先建立如下的一个page <!DOCTYPE html> <html xmlns=& ...
- puzz: 图片和表单上传的不一致问题
1. 方向1 用户提交表单, 图片和表单同步上传.(由同一服务器处理, 服务器压力大. 没有分离) 2. 方向2 图片和表单分开上传. 如图片访问ftp,表单提交后台(图片和后台分离) 2 ...
- [IDEA_3] IDEA 配置 GitHub 并上传项目
0. 说明 参考 Git & GitHub 的安装配置 IDEA 配置 GitHub 并上传项目 1. 安装配置 Git & GitHub 参照 Git & GitHub 的安 ...
- 本地项目通过github客户端上传到github网站上
一.github客户端上传步骤 一.github客户端上传步骤1.百度搜索关键字: GitHub 离线安装包 ==> Github_3.3.4.0版本链接:https://pan.baidu.c ...
- github客户端上传代码
在window下安装github客户端上传代码 第一步:创建Github新账户 第二步:新建仓库 第三步:安装Github shell程序,地址:http://windows.github.com/ ...
随机推荐
- ADT-位图
利用位图数据结构实现排序,利用每一位的下标作为索引,每位的值作为属性值,可以表示存在或不存在,适合存储稠密的数据,排序遍历的范围会是索引的最大值 最后发现耗时比python中list自带的sort多, ...
- LINUX 循环fork()
#include <sys/types.h> #include <unistd.h> int main() { for(int i = 0; i < ...
- C#读取文件高效方法实现
C# Code 12345678910111213141516171819202122232425262728293031 private void button1_Click ...
- linux 命令后台执行
我想把updatedb命令(用于重新建立整盘索引的命令)放在后台运行.因为我不想眼睁睁的看着机器建立索引,我还想编会儿程序呢: # updatedb & [1] 23336 注释:在所要执行的 ...
- android使用Genymotion作为模拟器
Genymotion模拟器的好处自然不用多说,直接来说怎么用: (1)去官方网站下载:中文或英文的: http://www.genymotion.net/ http://www.genymotion. ...
- bzoj1188
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1188 一道非常好的SG函数题,加深了对博弈论的理解. 以前做的SG函数的题,都是每个瓶子看成一 ...
- osip及eXosip的编译方法
osip及eXosip的编译方法 在最新版本的osip2.exosip2中不支持在VC6.0下编译osip.exosip协议栈的方法 说明: 1.以下文章中的osip版本为3.1.0 2.eXosip ...
- c3p0数据源定义
<!-- c3p0 connection pool configuration --> <bean id="testDataSource" class=" ...
- Laravel-高级篇-Auth-数据迁移-数据填充
(慕课网_轻松学会Laravel-高级篇_天秤vs永恒老师_http://www.imooc.com/learn/702) 一.生成Auth所需文件 在Artisan控制台输入以下命令 php art ...
- android 点击桌面图标,打开手机浏览器进入对应的站点
做一个假的adnroid app.要实现点击桌面图标.打开手机浏览器进入对应的站点,实现方法非常easy import android.app.Activity; import android.con ...