爬虫学习(三) Scrapy框架入门与豆瓣电影爬虫
2017-08-21 00:54
561 查看
知识点笔记
Scrapy框架知识点简记官方说明文档:http://scrapychs.readthedocs.io/zh_CN/latest/intro/tutorial.html(写在前面,必须要看的)
Scrapy是Python开发的一个快速web抓取的框架。需要提前安装一些库才可以安装Scrapy库。(1.lxml 2.zope.interface 3.Twisted 4.pyOpenSSL 5.pywin32 6.Scrapy)
Scrapy生成项目的方法:在cmd中用命令:scrapy startproject XXX
如下图所示:
在项目中引用库的方法如下所示:
from scrapy.spiders import CrawlSpider from scrapy .http import Request from scrapy.selector import Selector from movie.items import MovieItem
其生成的项目结构如图所示。在spiders目录下编写我们的爬虫文件
这里需要说明的是,在pycharm中运行scrapy框架需要一些设置,所以创建了main文件,其内部代码如图所示。(这里参考了其他相关博客,见链接http://blog.csdn.net/ck4438707/article/details/52076220)
scrapy各个文件功能,见官方说明文档不再赘述。
实例(豆瓣电影top250爬虫)
目标:url: http://movie.douban.com/top2502.
爬取内容:电影名称,信息,评分以及语录
保存为CSV文件
主程序(spider下面的内容):
#-*-coding:utf-8 -*- from scrapy.spiders import CrawlSpider from scrapy .http import Request from scrapy.selector import Selector from movie.items import MovieItem class test(CrawlSpider): name = "douban" redis_key='douban:start_urls' start_urls=['http://movie.douban.com/top250'] url='https://movie.douban.com/top250' def parse(self,response): #print response.body item = MovieItem() selector = Selector(response) Movies = selector.xpath('//div[@class="info"]') for eachMovie in Movies: title = eachMovie.xpath('div[@class="hd"]/a/span/text()').extract() fullTitle = '' for each in title: fullTitle += each movieInfo = eachMovie.xpath('div[@class="bd"]/p/text()').extract() star = eachMovie.xpath('div[@class="bd"]/div[@class="star"]/span[@class="rating_num"]/text()').extract()[0] quote = eachMovie.xpath('div[@class="bd"]/p[@class="quote"]/span/text()').extract() if quote: quote = quote[0] else: quote = '' item['title'] = fullTitle item['movieInfo'] = ';'.join(movieInfo) item['star'] = star item['quote'] = quote yield item nextlink = selector.xpath('//span[@class="next"]/link/@href').extract() if nextlink: nextlink = nextlink[0] print nextlink yield Request(self.url + nextlink, callback=self.parse)
items.py
from scrapy import Item, Field class MovieItem(Item): # define the fields for your item here like: # name = scrapy.Field() title = Field() movieInfo = Field() star = Field() quote = Field()
seting.py
# -*- coding: utf-8 -*- # Scrapy settings for movie project # # For simplicity, this file contains only settings considered important or # commonly used. You can find more settings consulting the documentation: # # http://doc.scrapy.org/en/latest/topics/settings.html # http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html # http://scrapy.readthedocs.org/en/latest/topics/spider-middleware.html BOT_NAME = 'movie' SPIDER_MODULES = ['movie.spiders'] NEWSPIDER_MODULE = 'movie.spiders' USER_AGENT='Mozilla/5.0 (Windows NT 10.0; Win64; x64)' # Crawl responsibly by identifying yourself (and your website) on the user-agent #USER_AGENT = 'movie (+http://www.yourdomain.com)' # Obey robots.txt rules ROBOTSTXT_OBEY = True # Configure maximum concurrent requests performed by Scrapy (default: 16) #CONCURRENT_REQUESTS = 32 # Configure a delay for requests for the same website (default: 0) # See http://scrapy.readthedocs.org/en/latest/topics/settings.html#download-delay # See also autothrottle settings and docs #DOWNLOAD_DELAY = 3 # The download delay setting will honor only one of: #CONCURRENT_REQUESTS_PER_DOMAIN = 16 #CONCURRENT_REQUESTS_PER_IP = 16 # Disable cookies (enabled by default) #COOKIES_ENABLED = False # Disable Telnet Console (enabled by default) #TELNETCONSOLE_ENABLED = False # Override the default request headers: #DEFAULT_REQUEST_HEADERS = { # 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', # 'Accept-Language': 'en', #} # Enable or disable spider middlewares # See http://scrapy.readthedocs.org/en/latest/topics/spider-middleware.html #SPIDER_MIDDLEWARES = { # 'movie.middlewares.MovieSpiderMiddleware': 543, #} # Enable or disable downloader middlewares # See http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html #DOWNLOADER_MIDDLEWARES = { # 'movie.middlewares.MyCustomDownloaderMiddleware': 543, #} # Enable or disable extensions # See http://scrapy.readthedocs.org/en/latest/topics/extensions.html #EXTENSIONS = { # 'scrapy.extensions.telnet.TelnetConsole': None, #} # Configure item pipelines # See http://scr 8d61 apy.readthedocs.org/en/latest/topics/item-pipeline.html #ITEM_PIPELINES = { # 'movie.pipelines.MoviePipeline': 300, #} # Enable and configure the AutoThrottle extension (disabled by default) # See http://doc.scrapy.org/en/latest/topics/autothrottle.html #AUTOTHROTTLE_ENABLED = True # The initial download delay #AUTOTHROTTLE_START_DELAY = 5 # The maximum download delay to be set in case of high latencies #AUTOTHROTTLE_MAX_DELAY = 60 # The average number of requests Scrapy should be sending in parallel to # each remote server #AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0 # Enable showing throttling stats for every response received: #AUTOTHROTTLE_DEBUG = False # Enable and configure HTTP caching (disabled by default) # See http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings #HTTPCACHE_ENABLED = True #HTTPCACHE_EXPIRATION_SECS = 0 #HTTPCACHE_DIR = 'httpcache' #HTTPCACHE_IGNORE_HTTP_CODES = [] #HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage' FEED_URI=u'file:///G:/movie.csv' FEED_FORMAT='CSV'
相关文章推荐
- Scrapy框架学习 - 爬取豆瓣电影排行榜TOP250所有电影信息并保存到MongoDB数据库中
- Python的爬虫程序编写框架Scrapy入门学习教程
- scrapy爬虫框架学习入门教程及实例
- python 爬虫 学习笔记(一)Scrapy框架入门
- python——爬虫学习——Scrapy爬虫框架入门-(6)
- 实践Python的爬虫框架Scrapy来抓取豆瓣电影TOP250
- 爬虫学习之scrapy框架入门
- Scrapy安装、爬虫入门教程、爬虫实例(豆瓣电影爬虫)
- Python 采用Scrapy爬虫框架爬取豆瓣电影top250
- 实践Python的爬虫框架Scrapy来抓取豆瓣电影TOP250
- 爬虫框架scrapy,爬取豆瓣电影top250
- Python的爬虫程序编写框架Scrapy入门学习教程
- scrapy爬虫框架教程(二)-- 爬取豆瓣电影TOP250
- python 爬虫学习三(Scrapy 实战,豆瓣爬取电影信息)
- Python爬虫从入门到放弃(十六)之 Scrapy框架中Item Pipeline用法
- [python爬虫入门]爬取豆瓣电影排行榜top250
- scrapy 爬虫框架学习(一、基础知识复习)
- 爬虫 scrapy 框架学习 1. Scrapy框架业务逻辑的理解 + 简单爬虫案例示范
- 爬虫框架Scrapy学习记录II--Selector学习
- Python爬虫入门 | 7 分类爬取豆瓣电影,解决动态加载问题