一个简单的scrapy爬虫抓取豆瓣刘亦菲的图片地址
一.第一步是创建一个scrapy项目
sh-3.2# scrapy startproject liuyifeiImage sh-3.2# chmod -R 777 liuyifeiImage/
二.分析图片特征
1.解决分页url部分:
我们爬虫的start_url是"http://movie.douban.com/celebrity/1049732/photos/?type=C&start=0&sortby=vote&size=a&subtype=a",
第二页地址是"http://movie.douban.com/celebrity/1049732/photos/?type=C&start=40&sortby=vote&size=a&subtype=a",
第三页是"http://movie.douban.com/celebrity/1049732/photos/?type=C&start=80&sortby=vote&size=a&subtype=a",能显而易见得到豆瓣图片的分页规则,因此我们的start_urls可以用一个for循环把所有的页面的url放进来。
start_urls = []; for i in range(0,1120,40):
start_urls.append('http://movie.douban.com/celebrity/1049732/photos/
type=C&start=%d&sortby=vote&size=a&subtype=a'%i)
2.解决每一页的图片url部分:
我们在"http://movie.douban.com/celebrity/1049732/photos/?type=C&start=0&sortby=vote&size=a&subtype=a"这一页来分析,审查第一张图片的页面元素
href对应的是每张图的大图地址,而<img src对应的是缩略图地址,我们来看看原图地址链接,
而根据href地址进入的页面图片地址为:
<img src="http://img3.douban.com/view/photo/photo/public/p752034364.jpg">
因此,显而易见,如果想要得到原图地址,只要吧".../view/photo/thumb/public/..."中的"thumb"替换成"photo"或者"raw"即可。
所以spider中的parse部分对应为:
def parse(self,response):
hxs=HtmlXPathSelector(response)
sites=hxs.select('//ul/li/div/a/img/@src').extract()
for site in sites:
#site=site.replace('thumb','photo')
site=site.replace('thumb','raw')
三.保存生成的url列表
在这里用了两种保存方式json和txt
1.先来看看txt保存方式:
f=open('liuyifei_pic_address.txt','wb')
def parse(self,response):
hxs=HtmlXPathSelector(response)
sites=hxs.select('//ul/li/div/a/img/@src').extract()
items=[]
for site in sites:
site=site.replace('thumb','raw')
self.f.write(site)
self.f.write('\r\n')
2.json保存:
直接在命令行里用参数执行即可:
scrapy crawl liuyifei -o image.json -t json
这样就能把url列表放置在本地文件image.json中,当然,运行scrapy时也是这条命令。
四.接下来,看看这个scrapy的全貌吧,主要修改的文件就是item.py和liuyifei.py(自己创建的spider文件)。
以下是items.py文件
#items.py from scrapy.item import Item,Field
class LiuyifeiimageItem(Item):
# define the fields for your item here like:
# name = scrapy.Field()
ImageAddress = Field()
pass
以下是liuyifei.py文件:
#liuyifei.py from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from liuyifeiImage.items import LiuyifeiimageItem '''http://movie.douban.com/celebrity/1049732/photos/''' class liuyifeiImage(BaseSpider):
name='liuyifei'
allowed_domain=["douban.com"]
start_urls=[]
f=open('liuyifei_pic_address.txt','wb')
for i in range(0,1120,40):
start_urls.append('http://movie.douban.com/celebrity/1049732/photos/?type=C&start=%d&sortby=vote&size=a&subtype=a'%i) def parse(self,response):
hxs=HtmlXPathSelector(response)
sites=hxs.select('//ul/li/div/a/img/@src').extract()
items=[]
for site in sites:
site=site.replace('thumb','raw')
self.f.write(site)
self.f.write('\r\n')
item=LiuyifeiimageItem()
item['ImageAddress']=site
items.append(item)
return items
最后,运行scrapy,以下是部分打印结果。
sh-3.2# scrapy crawl liuyifei -o image.json -t json /Users/lsf/PycharmProjects/liuyifeiImage/liuyifeiImage/spiders/liuyifei.py:8: ScrapyDeprecationWarning: liuyifeiImage.spiders.liuyifei.liuyifeiImage inherits from deprecated class scrapy.spider.BaseSpider, please inherit from scrapy.spider.Spider. (warning only on first subclass, there may be others)
class liuyifeiImage(BaseSpider):
2014-10-04 12:57:37+0800 [scrapy] INFO: Scrapy 0.24.4 started (bot: liuyifeiImage)
2014-10-04 12:57:37+0800 [scrapy] INFO: Optional features available: ssl, http11
2014-10-04 12:57:37+0800 [scrapy] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'liuyifeiImage.spiders', 'FEED_FORMAT': 'json', 'SPIDER_MODULES': ['liuyifeiImage.spiders'], 'FEED_URI': 'image.json', 'BOT_NAME': 'liuyifeiImage'}
2014-10-04 12:57:37+0800 [scrapy] INFO: Enabled extensions: FeedExporter, LogStats, TelnetConsole, CloseSpider, WebService, CoreStats, SpiderState
2014-10-04 12:57:37+0800 [scrapy] INFO: Enabled downloader middlewares: HttpAuthMiddleware, DownloadTimeoutMiddleware, UserAgentMiddleware, RetryMiddleware, DefaultHeadersMiddleware, MetaRefreshMiddleware, HttpCompressionMiddleware, RedirectMiddleware, CookiesMiddleware, ChunkedTransferMiddleware, DownloaderStats
2014-10-04 12:57:37+0800 [scrapy] INFO: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddleware
2014-10-04 12:57:37+0800 [scrapy] INFO: Enabled item pipelines:
2014-10-04 12:57:37+0800 [liuyifei] INFO: Spider opened
2014-10-04 12:57:37+0800 [liuyifei] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2014-10-04 12:57:37+0800 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6026
2014-10-04 12:57:37+0800 [scrapy] DEBUG: Web service listening on 127.0.0.1:6083
2014-10-04 12:57:38+0800 [liuyifei] DEBUG: Crawled (200) <GET http://movie.douban.com/celebrity/1049732/photos/?type=C&start=240&sortby=vote&size=a&subtype=a> (referer: None)
/Users/lsf/PycharmProjects/liuyifeiImage/liuyifeiImage/spiders/liuyifei.py:17: ScrapyDeprecationWarning: scrapy.selector.HtmlXPathSelector is deprecated, instantiate scrapy.Selector instead.
hxs=HtmlXPathSelector(response)
/Users/lsf/PycharmProjects/liuyifeiImage/liuyifeiImage/spiders/liuyifei.py:18: ScrapyDeprecationWarning: Call to deprecated function select. Use .xpath() instead.
sites=hxs.select('//ul/li/div/a/img/@src').extract()
/Library/Python/2.7/site-packages/Scrapy-0.24.4-py2.7.egg/scrapy/selector/unified.py:106: ScrapyDeprecationWarning: scrapy.selector.HtmlXPathSelector is deprecated, instantiate scrapy.Selector instead.
for x in result]
2014-10-04 12:57:38+0800 [liuyifei] DEBUG: Scraped from <200 http://movie.douban.com/celebrity/1049732/photos/?type=C&start=240&sortby=vote&size=a&subtype=a>
{'ImageAddress': u'http://img3.douban.com/view/photo/raw/public/p2179423125.jpg'}
2014-10-04 12:57:38+0800 [liuyifei] DEBUG: Scraped from <200 http://movie.douban.com/celebrity/1049732/photos/?type=C&start=240&sortby=vote&size=a&subtype=a>
{'ImageAddress': u'http://img3.douban.com/view/photo/raw/public/p2179423105.jpg'}
2014-10-04 12:57:38+0800 [liuyifei] DEBUG: Scraped from <200 http://movie.douban.com/celebrity/1049732/photos/?type=C&start=240&sortby=vote&size=a&subtype=a>
{'ImageAddress': u'http://img3.douban.com/view/photo/raw/public/p2179423084.jpg'} ... 2014-10-04 13:34:17+0800 [liuyifei] DEBUG: Scraped from <200 http://movie.douban.com/celebrity/1049732/photos/?type=C&start=1040&sortby=vote&size=a&subtype=a>
{'ImageAddress': u'http://img3.douban.com/view/photo/raw/public/p958573512.jpg'}
2014-10-04 13:34:17+0800 [liuyifei] DEBUG: Scraped from <200 http://movie.douban.com/celebrity/1049732/photos/?type=C&start=1040&sortby=vote&size=a&subtype=a>
{'ImageAddress': u'http://img5.douban.com/view/photo/raw/public/p958572938.jpg'}
2014-10-04 13:34:17+0800 [liuyifei] INFO: Closing spider (finished)
2014-10-04 13:34:17+0800 [liuyifei] INFO: Stored json feed (1120 items) in: image.json
2014-10-04 13:34:17+0800 [liuyifei] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 8331,
'downloader/request_count': 28,
'downloader/request_method_count/GET': 28,
'downloader/response_bytes': 221405,
'downloader/response_count': 28,
'downloader/response_status_count/200': 28,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2014, 10, 4, 5, 34, 17, 736723),
'item_scraped_count': 1120,
'log_count/DEBUG': 1150,
'log_count/INFO': 8,
'response_received_count': 28,
'scheduler/dequeued': 28,
'scheduler/dequeued/memory': 28,
'scheduler/enqueued': 28,
'scheduler/enqueued/memory': 28,
'start_time': datetime.datetime(2014, 10, 4, 5, 34, 14, 681268)}
2014-10-04 13:34:17+0800 [liuyifei] INFO: Spider closed (finished)
以下是json文件和txt文件:
image.json:
liuyifei_pic_address.txt
一个简单的scrapy爬虫抓取豆瓣刘亦菲的图片地址的更多相关文章
- Python爬虫----抓取豆瓣电影Top250
有了上次利用python爬虫抓取糗事百科的经验,这次自己动手写了个爬虫抓取豆瓣电影Top250的简要信息. 1.观察url 首先观察一下网址的结构 http://movie.douban.com/to ...
- 做一个简单的scrapy爬虫
前言: 做一个简单的scrapy爬虫,带大家认识一下创建scrapy的大致流程.我们就抓取扇贝上的单词书,python的高频词汇. 步骤: 一,新建一个工程scrapy_shanbay 二,在工程中中 ...
- 一个简单的python爬虫,爬取知乎
一个简单的python爬虫,爬取知乎 主要实现 爬取一个收藏夹 里 所有问题答案下的 图片 文字信息暂未收录,可自行实现,比图片更简单 具体代码里有详细注释,请自行阅读 项目源码: # -*- cod ...
- Python小爬虫——抓取豆瓣电影Top250数据
python抓取豆瓣电影Top250数据 1.豆瓣地址:https://movie.douban.com/top250?start=25&filter= 2.主要流程是抓取该网址下的Top25 ...
- 利用python scrapy 框架抓取豆瓣小组数据
因为最近在找房子在豆瓣小组-上海租房上找,发现搜索困难,于是想利用爬虫将数据抓取. 顺便熟悉一下Python. 这边有scrapy 入门教程出处:http://www.cnblogs.com/txw1 ...
- 一个简单的python爬虫,以豆瓣妹子“http://www.dbmeizi.com/category/2?p= ”为例
本想抓取网易摄影上的图,但发现查看html源代码时找不到图片的url,但firebug却能定位得到.(不知道为什么???) 目标是抓取前50页的爆乳图,代码如下: import urllib2,url ...
- python爬虫抓取豆瓣电影
抓取电影名称以及评分,并排序(代码丑炸) import urllib import re from bs4 import BeautifulSoup def get(p): t=0 k=1 n=1 b ...
- python实现的一个简单的网页爬虫
学习了下python,看了一个简单的网页爬虫:http://www.cnblogs.com/fnng/p/3576154.html 自己实现了一个简单的网页爬虫,获取豆瓣的最新电影信息. 爬虫主要是获 ...
- Scrapy爬虫入门系列4抓取豆瓣Top250电影数据
豆瓣有些电影页面需要登录才能查看. 目录 [隐藏] 1 创建工程 2 定义Item 3 编写爬虫(Spider) 4 存储数据 5 配置文件 6 艺搜参考 创建工程 scrapy startproj ...
随机推荐
- 关于在EXCEL中输入01-01-01被转换为2001/1/1怎么解决
当向EXCEL写入类似'01-01-01'或'01-01'这样的数据时,打开EXCEL时会发现数据变成了2001/1/1和1月1日. 这是由于EXCEL自动转换功能,我们得要在输入前多加一个’号. 而 ...
- SharePoint 2013 排错之"Code blocks are not allowed in this file"
今天,设置页面布局的自定义母版页时,设置完了以后保存,然后预览报错,错误如下截图:删掉自定义母版页的MasterPageFile属性,页面依然报错:感觉甚是奇怪,因为有版本控制,还原为最初的版本,依然 ...
- UDF2
问题 根据给定的gps点point(x,y)和北京的shape数据,关联出 AOI ID IO 输入 gps点表 create table gps ( x double, //经度 y double ...
- Android项目结构分析
andriod项目目录结构如下图: 1. src目录 该目录一个普通的保存java源文件的目录,其和普通java工程中的src目录是一样的. 2. gen目录 此目录用于存放所有由ADT插件自动生成的 ...
- 在Android开发中使用Ant 二:进行一次完整的打包
一次完整的Android打包要进行以下的几步:编译.代码混淆.打包apk.签名apk.apk优化. 为了能包涵使用NDK的情况,在这里使用一个有native代码的工程TestJni. 在工程根目录下新 ...
- iOS开发网络篇—NSURLConnection基本使用(一)
一.NSURLConnection的常用类 (1)NSURL:请求地址 (2)NSURLRequest:封装一个请求,保存发给服务器的全部数据,包括一个NSURL对象,请求方法.请求头.请求体.. ...
- android 之 spinner的简单使用
先看spinner的效果图: 代码: MainActivity package com.mecury.spinnertest; import java.util.ArrayList; import a ...
- windows 和 linux ssh互连
从windows连接到linux: linux开启sshd服务即可,主要是windows的配置如下: 1.使用软件,putty可以直接使用 2.使用cmd控制台连接linux,安装SSH Secure ...
- SQL2014内存表性能之内存中 OLTP 的性能改进测试
先贴1个例子,后续补充完整的测试例子.... 1.用MSDN例子测试一下 use master go --1.先创建包含内存优化文件组的数据库 CREATE DATABASE imoltp2 ON P ...
- SQL Server 2008 R2——VC++ ADO 操作 存储过程
==================================声明================================== 本文原创,转载在正文中显要的注明作者和出处,并保证文章的完 ...