利用scrapy框架python爬虫初探
来源:互联网 发布:马云淘宝 编辑:程序博客网 时间:2024/05/19 17:56
经过三天的“摸爬滚打”,终于搞定了一个简单的爬虫项目,因为个人初学爬虫,没有一个系统的框架很难完整爬一个项目,所以参照诸多教程与博客,终于拿下一个简单的爬取“伯乐在线”所有文章的爬虫。
1、准备工作——安装scrapy框架
Command "python setup.py egg_info" failed with error code 1 in
可通过此网站寻找解决方案,然后就可以新建我们的项目。2、开始爬取——新建scrapy项目
- scrapy startproject article
文件架构 - 开始编写爬虫代码
# -*- coding: utf-8 -*-import scrapyfrom scrapy import Spiderfrom ..items import ArticleItemclass 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 posts: 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.pyfrom openpyxl import Workbookclass 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 item
settings.pyITEM_PIPELINES = { 'article.pipelines.TuniuPipeline': 200, # 200是为了设置工序顺序}
启动爬虫 scrapy crawl article
结果
阅读全文
0 0
- 利用scrapy框架python爬虫初探
- Python 爬虫框架 scrapy
- Python爬虫框架--Scrapy
- Python Scrapy爬虫框架
- python爬虫 -- scrapy框架
- Python爬虫---scrapy框架
- <scrapy>python 爬虫框架scrapy安装
- Python网络爬虫框架:Scrapy
- python 爬虫scrapy框架练习
- Scrapy:Python的爬虫框架
- Python爬虫框架Scrapy安装
- Python的爬虫框架 Scrapy
- [Python]爬虫框架Scrapy人门
- python爬虫框架(scrapy)
- Scrapy:Python的爬虫框架
- Python scrapy爬虫框架简介
- Python爬虫框架Scrapy实战
- Python爬虫框架Scrapy实战
- 智慧警务指挥决策系统,助力平安城市
- Glide加载自签名认证的https图片
- 在linux环境下安装Mysql
- 选择相册上传图片 展示图片(仿微信选择图片)
- 浅谈web应用的负载均衡、集群、高可用(HA)解决方案(MARK)
- 利用scrapy框架python爬虫初探
- MongoDB实战-如何在Windows环境下管理副本集
- 关于matplotlib画图。中文乱码
- Spring IOC实现
- mysql存储过程
- C# WinForm窗体应用(第四天)
- EasyNVR H5无插件摄像机直播解决方案前端解析之:引用videojs无法自动播放
- JOB:面试基础2
- java中Date的一些操作