爬虫:股票数据Scrapy爬虫实例
2018-11-29 02:32
302 查看
版权声明:关注微信公众号:摸鱼科技资讯,联系我们 https://blog.csdn.net/qq_36949176/article/details/84595407
功能描述
*技术路线:scrapy
*目标:获取上交所和深交所所有的股票的名称和交易信息
*输出:保存到文件中
数据网站的确定,和之前博客相同:https://www.geek-share.com/detail/2754130660.html
实例编写:
步骤
步骤1:建立工程和Spider模板
cmd命令
[code]*\>scrapy startproject BaiduStocks *\>cd BaiduStocks *\>scrapy genspider stocks baidu.com *进一步修改spiders/stocks.py文件
步骤2:编写Spider
*配置stocks.py文件
*修改对返回页面的处理
*修改对新增URL爬取请求的处理
[code]# -*- coding: utf-8 -*- import scrapy import re class StocksSpider(scrapy.Spider): name = 'stocks' start_urls = ['http://quote.eastmoney.com/stocklist.html'] def parse(self, response): for href in response.css('a::attr(href)').extract(): try: stock=re.findall(r"[s][hz]\d{6}",href)[0] url='https://gupiao.baidu.com/stock/'+stock+'.html' yield scrapy.Request(url,callback=self.parse_stock) except: continue def parse_stock(self,response): infoDict={} stockInfo=response.css('.stock-bets') name=stockInfo.css('.bets-name').extract()[0] keyList=stockInfo.css('dt').extract() ValueList=stockInfo.css('dd').extract() for i in range(len(keyList)): key=re.findall(r'>.*</dt>',keyList[i])[0][1:-5] try: val=re.findall(r'\d+\.?.*</dd>',valueList[i])[0][0:-5] except: val='--' infoDict[key]=val infoDict.update( {'股票名称':re.findall('\s.*\(',name)[0].split()[0]+\ re.findall('\>.*\<',name)[0][1:-1]}) yield infoDict
步骤3:编写ITEM Pipelines
*配置pipelines.py文件
*定义对爬取项(Scraped Item)的处理类
*配置ITEM_PIPELINES选项
配置和定义类
[code]# -*- coding: utf-8 -*- # Define your item pipelines here # # Don't forget to add your pipeline to the ITEM_PIPELINES setting # See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html class BaidustocksPipeline(object): def process_item(self, item, spider): return item class BaidustocksInfoPipeline(object): def open_spider(self,spider): self.f=open('BaiduStockInfo.txt','w') def close_spider(self,spider): self.f.close() def process_item(self,item,spider): try: line=str(dict(item))+'\n' self.f.write(line) except: pass return item
我们重新写了并命名了BaidustocksInfoPipeline这个类,然后我们需要修改设置setting的配置文件,将新写的类添加到程序中运行中去,我们在settings.py中找到ITEM_PIPELINES,取消注释并把里面的参数修改为自己重新写的这个类
[code]ITEM_PIPELINES = { 'BaiduStocks.pipelines.BaidustocksInfoPipeline': 300, }
然后我们在stocks.py目录下,打开cmd,执行crawl命令运行程序
[code]scrapy crawl stocks
运行成功后生成txt文件数据储存在里面如下图所示
阅读更多
相关文章推荐
- Python网络爬虫与信息提取-Day14-(实例)股票数据定向爬虫
- 数据分析——以斗鱼为实例解析requests库与scrapy框架爬虫技术
- Scrapy爬虫(七):爬虫数据存储实例
- scrapy爬虫之item pipeline保存数据
- Scrapy爬虫笔记【5-实例一:爬豆瓣】
- Java网络爬虫基础和抓取网站数据的两个小实例
- 搭建scrapy抓取javascript动态数据的爬虫
- Scrapy安装、爬虫入门教程、爬虫实例(豆瓣电影爬虫)
- Python3 大型网络爬虫实战 002 --- scrapy 爬虫项目的创建及爬虫的创建 --- 实例:爬取百度标题和CSDN博客
- scrapy爬虫实例w3school报错ImportError: No module named w3school.items
- 爬虫学习实例 - selenium Webdirver爬取集思录债券数据
- Python爬虫框架Scrapy实例
- scrapy爬虫框架实例二
- 处理XML实例-股票历史数据下载
- 互联网金融爬虫怎么写-第四课 雪球网股票爬虫(单页面多数据)
- python爬虫"Hello World"级入门实例(二),使用json从中国天气网抓取数据
- 数据抓取学习(爬虫via scrapy)
- scrapy框架爬虫,爬取腾讯职业实例
- Scrapy 爬虫框架爬取网页数据
- 分布式爬虫:使用Scrapy抓取数据