利用scrapy框架python爬虫初探
2017-08-30 09:37
633 查看
经过三天的“摸爬滚打”,终于搞定了一个简单的爬虫项目,因为个人初学爬虫,没有一个系统的框架很难完整爬一个项目,所以参照诸多教程与博客,终于拿下一个简单的爬取“伯乐在线”所有文章的爬虫。
1、准备工作——安装scrapy框架
可通过此网站寻找解决方案,然后就可以新建我们的项目。
2、开始爬取——新建scrapy项目
scrapy startproject article
文件架构
开始编写爬虫代码
存入excel
piprlines.py
from openpyxl import Workbook
class TuniuPipeline(object): # 设置工序一
wb = Workbook()
ws = wb.active
ws.append(['标题', '链接', '发布时间', '简介']) # 设置表头
def process_item(self, item, spider): # 工序具体内容
line = [item['title'], item['link'], item['date'], item['short']] # 把数据中每一项整理出来
self.ws.append(line) # 将数据以行的形式添加到xlsx中
self.wb.save('article.xlsx') # 保存xlsx文件
return itemsettings.py
启动爬虫 scrapy crawl article
结果
1、准备工作——安装scrapy框架
Command "python setup.py egg_info" failed with error code 1 in
可通过此网站寻找解决方案,然后就可以新建我们的项目。
2、开始爬取——新建scrapy项目
scrapy startproject article
文件架构
开始编写爬虫代码
# -*- coding: utf-8 -*- import scrapy from scrapy import Spider from ..items import ArticleItem class ArticleSpider(scrapy.Spider): name = 'article' start_urls = ['http://python.jobbole.com/all-posts/'] def parse(self, response): item = ArticleItem() posts = response.xpath('//div[@class="post floated-thumb"]') # print(posts) for post in post 944f s: item['title'] = post.xpath('.//a[@class="archive-title"]/text()').extract()[0] # print(item['title']) item['date'] = post.xpath('.//div[@class="post-meta"]/p/text()').re('\d+/+\d+/+\d+')[0] # print(item['date']) item['short'] = post.xpath('.//span[@class="excerpt"]/p/text()').extract()[0] # print(item['short']) item['link'] = post.xpath('.//span[@class="read-more"]/a/@href').extract()[0] print(item['title']+item['date']+item['short']+item['link']) yield item urls = response.xpath('//a[@class="next page-numbers"]/@href').extract()[0] if urls: yield scrapy.Request(urls, callback=self.parse)
存入excel
piprlines.py
from openpyxl import Workbook
class TuniuPipeline(object): # 设置工序一
wb = Workbook()
ws = wb.active
ws.append(['标题', '链接', '发布时间', '简介']) # 设置表头
def process_item(self, item, spider): # 工序具体内容
line = [item['title'], item['link'], item['date'], item['short']] # 把数据中每一项整理出来
self.ws.append(line) # 将数据以行的形式添加到xlsx中
self.wb.save('article.xlsx') # 保存xlsx文件
return itemsettings.py
ITEM_PIPELINES = { 'article.pipelines.TuniuPipeline': 200, # 200是为了设置工序顺序 }
启动爬虫 scrapy crawl article
结果
相关文章推荐
- 数据可视化 三步走(一):数据采集与存储,利用python爬虫框架scrapy爬取网络数据并存储
- 小猪的Python学习之旅 —— 4.Scrapy爬虫框架初体验
- Python3.6安装Scrapy爬虫框架
- Python3环境安装Scrapy爬虫框架过程及常见错误
- python爬虫框架scrapy实例详解
- python爬虫框架scrapy实例详解
- 【实战\聚焦Python分布式爬虫必学框架Scrapy 打造搜索引擎项目笔记】第1章 课程介绍
- python爬虫scrapy框架——人工识别登录知乎倒立文字验证码和数字英文验证码(2)
- Python爬虫进阶一之爬虫框架Scrapy安装配置
- Python爬虫框架Scrapy安装使用步骤
- Python爬虫框架Scrapy常用命令总结
- Python爬虫框架Scrapy 学习笔记 10.1 -------【实战】 抓取天猫某网店所有宝贝详情
- Python爬虫框架Scrapy实战之定向批量获取职位招聘信息
- [Python]网络爬虫(11):亮剑!爬虫框架小抓抓Scrapy闪亮登场!
- python开源爬虫框架scrapy源码解析(二)
- Scrapy框架利用CrawlSpider创建自动爬虫
- python爬虫由浅入深12---scrapy框架的基础入门
- Python爬虫框架Scrapy教程(1)—入门
- Python 采用Scrapy爬虫框架爬取豆瓣电影top250
- Python爬虫框架Scrapy实战教程---定向批量获取职位招聘信息