Skip to content

Commit a069253

Browse files
committed
🎉 add stock readme
1 parent 8691176 commit a069253

3 files changed

Lines changed: 30 additions & 6 deletions

File tree

‎by-bs4/stock/README.md‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# stock
2+
3+
一个基于网易财经的股票爬虫,可以爬取任意股票的所有历史交易数据
4+
5+
基本上只需要输入三个参数:
6+
7+
股票的代码:如601857(中国石油)
8+
9+
爬取的起始年份:2007
10+
11+
爬取的结束年份:2016
12+
13+
不用担心某一年份的某一季度数据为空的情况,不会报错,不会存入空数据,会直接跳过。
14+
考虑到之后会用pandas或者别的库处理csv文件,文件名含有中文可能比较麻烦。所以爬取所有数据的爬虫分为两个:只有股票代码 和 股票代码+中文名字。
15+
16+
- 获取该支股票的所有交易数据并写入以其股票代码命名的csv中 [stock2csvALL.py](stock2csvALL.py)
17+
18+
- 获取该支股票的所有交易数据并写入以其股票代码和名字命名的csv中 [stock2csvALLWithName.py](stock2csvALLWithName.py)
19+
20+
下面是稍微非主流一些的文件:
21+
22+
- 获取该支股票的指定年度指定季度的数据并写入csv [stock2csv.py](stock2csv.py)
23+
24+
- 获取该支股票的所有交易数据并写入txt [stock2txt.py](stock2txt.py)

‎by-bs4/stock/stock2csv.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def sharesCrawl(shareCode,year,season):
2323
table = soup.findAll('table',{'class':'table_bg001'})[0]
2424
rows = table.findAll('tr')
2525

26-
csvFile = open('./' + shareCodeStr + '.csv', 'wb')
26+
csvFile = open('./data/' + shareCodeStr + 'Y' + yearStr + 'S' + seasonStr + '.csv', 'wb')
2727
writer = csv.writer(csvFile)
2828

2929
writer.writerow(('日期', '开盘价', '最高价', '最低价', '收盘价', '涨跌额', '涨跌幅', '成交量', '成交金额', '振幅', '换手率'))

‎by-bs4/stock/stock2txt.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ def sharesCrawl2(shareCode,year,season):
6464

6565

6666
def createUrl(shareCode,beginYear,endYear):
67-
title = str(shareCode)
67+
shareCodeStr = str(shareCode)
6868

69-
if os.path.exists('./'+title) == False:
70-
#create the share folder
71-
os.mkdir('./'+title)
69+
# if os.path.exists('./'+title) == False:
70+
# #create the share folder
71+
# os.mkdir('./'+title)
7272

73-
f = open('./' + title + '.txt', 'wb')
73+
f = open('./' + shareCodeStr + '.txt', 'wb')
7474

7575
try:
7676
for i in range(beginYear,endYear+1):

0 commit comments

Comments
 (0)