-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNote for web crawler
More file actions
338 lines (293 loc) · 10.5 KB
/
Note for web crawler
File metadata and controls
338 lines (293 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
1. “迷你爬虫编程小练习”进阶:抽取某本书的前50条短评内容并计算评分的平均值。
import requests, re, time
from bs4 import BeautifulSoup
count = 0
i = 0
sum, count_s = 0, 0
while(count < 50):
try:
r = requests.get('https://book.douban.com/subject/bookid/comments/hot?p=' + str(i+1))
except Exception as err:
print(err)
break
soup = BeautifulSoup(r.text, 'lxml')
comments = soup.find_all('p', 'comment-content')
for item in comments:
count = count + 1
print(count, item.string)
if count == 50:
break
pattern = re.compile('<span class="user-stars allstar(.*?) rating"')
p = re.findall(pattern, r.text)
for star in p:
count_s = count_s + 1
sum += int(star)
time.sleep(5) # delay request from douban's robots.txt
i += 1
if count == 50:
print(sum / count_s)
#count=50,count_sb不一定等于50,一般是多余50,取决于抓取的网页上总共的comments的数量!!
2.在“http://money.cnn.com/data/dow30/”上抓取道指成分股数据并将30家公司的代码、公司名称和最近一次成交价放到一个列表中输出。
code:
import requests
import re
def retrieve_dji_list():
r = requests.get('http://money.cnn.com/data/dow30/')
search_pattern = re.compile('class="wsod_symbol">(.*?)<\/a>.*<span.*">(.*?)<\/span>.*\n.*class="wsod_stream">(.*?)<\/span>')
dji_list_in_text = re.findall(search_pattern, r.text)
return dji_list_in_text
dji_list = retrieve_dji_list()
print(dji_list)
3
.定义函数countchar()按字母表顺序统计字符串中26个字母出现的次数(不区分大小写)。例如字符串“Hope is a good thing.”的统计结果为:
[1, 0, 0, 1, 1, 0, 2, 2, 2, 0, 0, 0, 0, 1, 3, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0]
Code
def countchar(s):
lst = [0] * 26
for i in range(len(s)):
if s[i] >= 'a' and s[i] <='z':
lst[ord(s[i])- ord('a')] += 1
print(lst)
if __name__ == "__main__":
s = "Hope is a good thing."
s = s.lower()
countchar(s)
4. 请完成以下文件综合编程迷你项目(提示:可以利用list的insert函数)。
(1) 创建一个文件Blowing in the wind.txt,其内容是:
How many roads must a man walk down
Before they call him a man
How many seas must a white dove sail
Before she sleeps in the sand
How many times must the cannon balls fly
Before they're forever banned
The answer my friend is blowing in the wind
The answer is blowing in the wind
(2) 在文件头部插入歌名“Blowin’ in the wind”
(3) 在歌名后插入歌手名“Bob Dylan”
(4) 在文件末尾加上字符串“1962 by Warner Bros. Inc.”
(5) 在屏幕上打印文件内容
Code:def insert_line(lines):
lines.insert(0, "Blowin' in the wind\n")
lines.insert(1, "Bob Dylan\n")
lines.append("1962 by Warner Bros. Inc.")
return ''.join(lines)
with open('Blowing in the wind.txt', 'r+') as f:
lines = f.readlines()
string = insert_line(lines)
print(string)
f.seek(0)
f.write(string)
请将Intel和IBM公司近一年来每个月开票价的平均值绘制在一张图中(用subplot)。
import requests
import re
import json
import pandas as pd
from datetime import date
import time
from pylab import *
from scipy.cluster.vq import *
def retrieve_quotes_historical(stock_code):
quotes = []
url = 'https://finance.yahoo.com/quote/%s/history?p=%s' % (stock_code, stock_code)
r = requests.get(url)
m = re.findall('"HistoricalPriceStore":{"prices":(.*?),"isPending"', r.text)
if m:
quotes = json.loads(m[0])
quotes = quotes[::-1]
return [item for item in quotes if not 'type' in item]
def create_aveg_open(stock_code):
quotes = retrieve_quotes_historical(stock_code)
list1 = []
for i in range(len(quotes)):
x = date.fromtimestamp(quotes[i]['date'])
y = date.strftime(x,'%Y-%m-%d')
list1.append(y)
quotesdf_ori = pd.DataFrame(quotes, index = list1)
listtemp = []
for i in range(len(quotesdf_ori)):
temp = time.strptime(quotesdf_ori.index[i],"%Y-%m-%d")
listtemp.append(temp.tm_mon)
tempdf = quotesdf_ori.copy()
tempdf['month'] = listtemp
meanopen = tempdf.groupby('month').open.mean()
return meanopen
open1 = create_aveg_open('INTC')
open2 = create_aveg_open('IBM')
subplot(211)
plt.plot(open1.index,open1.values,color='r',marker='o')
subplot(212)
plt.plot(open1.index,open2.values,color='green',marker='o')
# Filename: kmeansDJI.py
import requests
import re
import json
import pandas as pd
from sklearn.cluster import KMeans
import numpy as np
def retrieve_quotes_historical(stock_code):
quotes = []
url = 'https://finance.yahoo.com/quote/%s/history?p=%s' % (stock_code, stock_code)
r = requests.get(url)
m = re.findall('"HistoricalPriceStore":{"prices":(.*?),"isPending"', r.text)
if m:
quotes = json.loads(m[0])
quotes = quotes[::-1]
return [item for item in quotes if not 'type' in item]
def create_df(stock_code):
quotes = retrieve_quotes_historical(stock_code)
list1 = ['close','date','high','low','open','volume']
df_totalvolume = pd.DataFrame(quotes,columns=list1)
return df_totalvolume
listDji = ['MMM','AXP','AAPL','BA','CAT','CVX','CSCO','KO','DIS','DD']
listTemp = [0] * len(listDji)
for i in range(len(listTemp)):
listTemp[i] = create_df(listDji[i]).close
status = [0] * len(listDji)
for i in range(len(status)):
status[i] = np.sign(np.diff(listTemp[i]))
kmeans = KMeans(n_clusters = 3).fit(status)
pred=kmeans.predict(status)
print(pred)
# Filename: plotKO.py
import requests
import re
import json
import pandas as pd
from datetime import date
import time
import matplotlib.pyplot as plt
def retrieve_quotes_historical(stock_code):
quotes = []
url = 'https://finance.yahoo.com/quote/%s/history?p=%s' % (stock_code, stock_code)
r = requests.get(url)
m = re.findall('"HistoricalPriceStore":{"prices":(.*?),"isPending"', r.text)
if m:
quotes = json.loads(m[0])
quotes = quotes[::-1]
return [item for item in quotes if not 'type' in item]
quotes = retrieve_quotes_historical('KO')
list1 = []
for i in range(len(quotes)):
x = date.fromtimestamp(quotes[i]['date'])
y = date.strftime(x,'%Y-%m-%d')
list1.append(y)
quotesdf_ori = pd.DataFrame(quotes, index = list1)
quotesdf = quotesdf_ori.drop(['date'], axis = 1)
listtemp = []
for i in range(len(quotesdf)):
temp = time.strptime(quotesdf.index[i],"%Y-%m-%d")
listtemp.append(temp.tm_mon)
tempdf = quotesdf.copy()
tempdf['month'] = listtemp
closeMeansKO = tempdf.groupby('month').close.mean()
x = closeMeansKO.index
y = closeMeansKO.values
plt.plot(x, y)
plt.savefig('1.jpg')
# Filename: plot_volumes.py
import requests
import re
import json
import pandas as pd
from datetime import date
import time
from pylab import *
from scipy.cluster.vq import *
def retrieve_quotes_historical(stock_code):
quotes = []
url = 'https://finance.yahoo.com/quote/%s/history?p=%s' % (stock_code, stock_code)
r = requests.get(url)
m = re.findall('"HistoricalPriceStore":{"prices":(.*?),"isPending"', r.text)
if m:
quotes = json.loads(m[0])
quotes = quotes[::-1]
return [item for item in quotes if not 'type' in item]
def create_volumes(stock_code):
quotes = retrieve_quotes_historical(stock_code)
list1 = []
for i in range(len(quotes)):
x = date.fromtimestamp(quotes[i]['date'])
y = date.strftime(x,'%Y-%m-%d')
list1.append(y)
quotesdf_ori = pd.DataFrame(quotes, index = list1)
listtemp = []
for i in range(len(quotesdf_ori)):
temp = time.strptime(quotesdf_ori.index[i],"%Y-%m-%d")
listtemp.append(temp.tm_mon)
tempdf = quotesdf_ori.copy()
tempdf['month'] = listtemp
totalvolume = tempdf.groupby('month').volume.sum()
return totalvolume
INTC_volumes = create_volumes('INTC')
IBM_volumes = create_volumes('IBM')
quotesIIdf = pd.DataFrame()
quotesIIdf['INTC'] = INTC_volumes
quotesIIdf['IBM'] = IBM_volumes
quotesIIdf.plot(kind = 'bar')
# Filename: stock.py
import requests
import re
import pandas as pd
def retrieve_dji_list():
r = requests.get('http://money.cnn.com/data/dow30/')
search_pattern = re.compile('class="wsod_symbol">(.*?)<\/a>.*<span.*">(.*)<\/span>.*\n.*class="wsod_stream">(.*)<\/span>')
dji_list_in_text = re.findall(search_pattern, r.text)
dji_list = []
for item in dji_list_in_text:
dji_list.append([item[0], item[1], float(item[2])])
return dji_list
dji_list = retrieve_dji_list()
djidf = pd.DataFrame(dji_list)
cols = ['code', 'name', 'lasttrade']
djidf.columns = cols
print(djidf)
# Filename: quotes_history_v2.py
import requests
import re
import json
import pandas as pd
from datetime import date
def retrieve_quotes_historical(stock_code):
quotes = []
url = 'https://finance.yahoo.com/quote/%s/history?p=%s' % (stock_code, stock_code)
r = requests.get(url)
m = re.findall('"HistoricalPriceStore":{"prices":(.*?),"isPending"', r.text)
if m:
quotes = json.loads(m[0])
quotes = quotes[::-1]
return [item for item in quotes if not 'type' in item]
quotes = retrieve_quotes_historical('IBM')
list1 = []
for i in range(len(quotes)):
x = date.fromtimestamp(quotes[i]['date'])
y = date.strftime(x,'%Y-%m-%d')
list1.append(y)
quotesdf_ori = pd.DataFrame(quotes, index = list1)
quotesdf = quotesdf_ori.drop(['date'], axis = 1)
print(quotesdf)
######经典机器学习
# 利用KNN分类算法进行分类
from sklearn import neighbors , datasets
iris = datasets.load_iris()
knn = neighbors.KNeighborsClassifier()
# 从已有数据中学习
knn.fit(iris.data, iris.target)
# 利用分类模型进行未知数据的预测(确定标签)
print(knn.predict([[5.0, 3.0, 5.0, 2.0]]))
# 利用k-means聚类算法进行聚类
from sklearn import cluster, datasets
iris = datasets.load_iris()
kmeans = cluster.KMeans(n_clusters = 3).fit(iris.data)
pred = kmeans.predict(iris.data) # 确定数据的类别
# 比较算法正确率
for label in pred:
print(label, end = ' ') # 打印预测出的各条数据的标签
print('\n')
for label in iris.target:
print(label, end = ' ') # 打印原始标注好的正确标签
#SVM(Support Vector Machine,支持向量机)分类算法对数据进行分类:
rom sklearn import svm, datasets
iris = datasets.load_iris()
svc = svm.LinearSVC()
svc.fit(iris.data, iris.target) # 学习
svc.predict([[ 5.0, 3.0, 5.0, 2.0]]) # 预测