Python网络爬虫实例2:Scrapy框架爬取股票数据
一、功能描述
目标:获取上交所和深交所所有股票的名称和交易信息
输出:保存到文件中
技术路线:scrapy
二、数据网站的确定
-
获取股票列表:
东方财富网-http://quote.eastmoney.com/stocklist.html -
获取个股信息:
- 雪球:https://xueqiu.com/S/
- 单个股票:https://xueqiu.com/S/SH600198
三、股票数据Scrapy爬虫实例编写
-
建立工程和spider模板
\>scrapy startproject XueqiuStocks
\>scrapy genspider stocks xueqiu.com
-
编写spider
配置spider.py
# -*- coding: utf-8 -*- import scrapy import re class StocksSpider(scrapy.Spider): name = 'stocks' start_urls = ['http://quote.eastmoney.com/stock_list.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 = <