Python——東方財富股民數據分析


一、選題背景

為什么要選擇此選題?要達到的數據分析的預期目標是什么?(10 分)

通過網絡爬蟲爬取股民信息,並且對爬取的數據進行進一步清洗處理,提取可利用數據信息。

二、主題式網絡爬蟲設計方案(10 分)

1.網絡爬蟲名稱:“東方財富股民數據分析”。

2.網絡爬蟲爬取的內容與數據特征分析:

通過網絡爬蟲技術分析該網站網頁結構獲取東方財富股吧發帖數據,  獲取多維度數據,並且經過數據清洗分析,獲取所需數據,經過清洗后的數據無重復值無空值,使數據更加可靠。

3.網絡爬蟲設計方案概述:

需多個步驟實現:

通過獲取網頁資源,設置請求頭,防止被網頁識別爬蟲,利用requests請求,使用etree解析網頁,定位爬取資源將數據保存到csv文件中。

3.主題式網絡爬蟲設計方案概述(包括實現思路與技術難點)

 

三、主題頁面的結構特征分析(10 分)

1.主題頁面的結構與特征分析

數據來源:http://guba.eastmoney.com/list,002460_1.html

 

2.Htmls 頁面解析

所需頁面代碼:

四、網絡爬蟲程序設計(60 分)

爬蟲程序主體要包括以下各部分,要附源代碼及較詳細注釋,並在每部分程序后

面提供輸出結果的截圖。

1.數據爬取與采集

 1 headers = {
 2 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36 Edg/90.0.818.66'
 3 }
 4 for u in range(1,10):
 5 
 6 url = f'http://guba.eastmoney.com/list,002460_{u}.html'
 7 
 8 
 9 res = requests.get(url=url,headers=headers,allow_redirects=False).text
10 
11 tree = etree.HTML(res)
12 
13 href = tree.xpath('//*[@class="articleh normal_post"]/span[3]/a/@href')
14 for i,j in enumerate(href):
15 
16 new_url = 'http://guba.eastmoney.com'+j
17 rea = requests.get(url=new_url,headers=headers)
18 rea.encoding = 'utf-8'
19 rea = rea.text
20 
21 try:
22 ex_content = re.findall('var post_article = (.*?);',rea,re.S)
23 content = json.loads(ex_content[0])
24 text +=content['post']['post_content']
25 
26 print(content['post']['post_content'])
27 except:
28 pass

 

 

 

2.對數據進行清洗和處理

 

 1 data_dict ={}
 2 
 3 for i in data_list:
 4 shu = text.count(i)
 5 data_dict[i] = shu
 6 
 7 # 切割爬取下來的評論
 8 words = jieba.lcut(text,cut_all=True)
 9 
10 
11 # 聲明一個字典,為詞雲做准備
12 counts = {}
13 
14 # 排除長度為1的單詞或標點
15 for word in words:
16 pattern = '[\u4e00-\u9fff]+' # 漢字正則表達式
17 re_compile = re.compile(pattern)
18 
19 res_text = re_compile.findall(word)
20 
21 if len(res_text) == 0:
22 pass
23 elif len(word) == 1:
24 pass
25 else:
26 # 整合到之前申明的字典中,並且附帶出現的次數,為了詞雲做准備
27 counts[word] = counts.get(word,0)+1
28 
29 print(counts)
30 lists = []
31 # 轉為list格式,詞雲接受list類型
32 item = list(counts.items())
33 
34 # 聲明個匿名函數給list從出現的次數從高到低進行排序
35 item.sort(key= lambda x:x[1],reverse=True)
36 
37 # 循環為了排除不需要的值
38 for j,i in enumerate(item):
39 # 只要前三的值
40 if j<3:
41 # 加入到一個新的列表
42 lists.append(i)
43 
44 print(item)

 

3.文本分析(可選):jieba 分詞、wordcloud 的分詞可視化

1 plt.rcParams['font.sans-serif'] = ['SimHei']
2 plt.rcParams['axes.unicode_minus'] = False
3 plt.bar(data_dict.keys(), data_dict.values(), 0.4, color="green")
4 plt.xticks(rotation=30) # 傾斜70度
5 plt.xlabel('關鍵詞')
6 plt.ylabel('出現次數')
7 plt.title('對一些關鍵詞統計直方圖')
8 
9 plt.show()

 

 

4.數據分析與可視化(例如:數據柱形圖、直方圖、散點圖、盒圖、分布圖)

 1 # 給詞雲初始化,並且設定寬高
 2 mywordcloud = WordCloud(InitOpts(width='800px',height='500px') )
 3 
 4 # 給詞雲加入數據和設定單詞大小和詞雲的圖形
 5 mywordcloud.add('',item,word_size_range=[30,70], shape='circle')
 6 
 7 # 保存到文件夾
 8 mywordcloud.render('D://wordcloud.html')
 9 print()
10 print()
11 print(data_dict)
12 
13 plt.rcParams['font.sans-serif'] = ['SimHei']
14 plt.rcParams['axes.unicode_minus'] = False
15 plt.bar(data_dict.keys(), data_dict.values(), 0.4, color="green")
16 plt.xticks(rotation=30) # 傾斜70度
17 plt.xlabel('關鍵詞')
18 plt.ylabel('出現次數')
19 plt.title('對一些關鍵詞統計直方圖')
20 
21 plt.show()

 

5.將以上各部分的代碼匯總,附上完整程序代碼

  1 import jieba
  2 import requests
  3 from lxml import etree
  4 import re
  5 import json
  6 
  7 from matplotlib import pyplot as plt
  8 from pyecharts.charts import WordCloud
  9 from pyecharts.options import InitOpts
 10 
 11 text = ''
 12 data_list = ['還會漲','大漲','大跌','看多','看空','守住','拿着','不錯','很好','挺好的','止損','止盈','被套了']
 13 
 14 headers = {
 15 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36 Edg/90.0.818.66'
 16 }
 17 for u in range(1,10):
 18 
 19 url = f'http://guba.eastmoney.com/list,002460_{u}.html'
 20 
 21 
 22 res = requests.get(url=url,headers=headers,allow_redirects=False).text
 23 
 24 tree = etree.HTML(res)
 25 
 26 href = tree.xpath('//*[@class="articleh normal_post"]/span[3]/a/@href')
 27 for i,j in enumerate(href):
 28 
 29 new_url = 'http://guba.eastmoney.com'+j
 30 rea = requests.get(url=new_url,headers=headers)
 31 rea.encoding = 'utf-8'
 32 rea = rea.text
 33 
 34 try:
 35 ex_content = re.findall('var post_article = (.*?);',rea,re.S)
 36 content = json.loads(ex_content[0])
 37 text +=content['post']['post_content']
 38 
 39 print(content['post']['post_content'])
 40 except:
 41 pass
 42 print(text)
 43 data_dict ={}
 44 
 45 for i in data_list:
 46 shu = text.count(i)
 47 data_dict[i] = shu
 48 
 49 # 切割爬取下來的評論
 50 words = jieba.lcut(text,cut_all=True)
 51 
 52 
 53 # 聲明一個字典,為詞雲做准備
 54 counts = {}
 55 
 56 # 排除長度為1的單詞或標點
 57 for word in words:
 58 pattern = '[\u4e00-\u9fff]+' # 漢字正則表達式
 59 re_compile = re.compile(pattern)
 60 
 61 res_text = re_compile.findall(word)
 62 
 63 if len(res_text) == 0:
 64 pass
 65 elif len(word) == 1:
 66 pass
 67 else:
 68 # 整合到之前申明的字典中,並且附帶出現的次數,為了詞雲做准備
 69 counts[word] = counts.get(word,0)+1
 70 
 71 print(counts)
 72 lists = []
 73 # 轉為list格式,詞雲接受list類型
 74 item = list(counts.items())
 75 
 76 # 聲明個匿名函數給list從出現的次數從高到低進行排序
 77 item.sort(key= lambda x:x[1],reverse=True)
 78 
 79 # 循環為了排除不需要的值
 80 for j,i in enumerate(item):
 81 # 只要前三的值
 82 if j<3:
 83 # 加入到一個新的列表
 84 lists.append(i)
 85 
 86 print(item)
 87 # 給詞雲初始化,並且設定寬高
 88 mywordcloud = WordCloud(InitOpts(width='800px',height='500px') )
 89 
 90 # 給詞雲加入數據和設定單詞大小和詞雲的圖形
 91 mywordcloud.add('',item,word_size_range=[30,70], shape='circle')
 92 
 93 # 保存到文件夾
 94 mywordcloud.render('D://wordcloud.html')
 95 print()
 96 print()
 97 print(data_dict)
 98 
 99 plt.rcParams['font.sans-serif'] = ['SimHei']
100 plt.rcParams['axes.unicode_minus'] = False
101 plt.bar(data_dict.keys(), data_dict.values(), 0.4, color="green")
102 plt.xticks(rotation=30) # 傾斜70度
103 plt.xlabel('關鍵詞')
104 plt.ylabel('出現次數')
105 plt.title('對一些關鍵詞統計直方圖')
106 
107 plt.show()

 

五、總結(10 分)

1.經過對主題數據的分析與可視化,可以得到哪些結論?是否達到預期的目標?

(1)可以通過數據可視化的詞雲和直方圖的頻率可以看出股民對股市的一些看法。

(2)經過數據清洗分析后,我們可以快速直觀的了解到所需數據。

2.在完成此設計過程中,得到哪些收獲?以及要改進的建議?

通過此次課程設計,使我更加扎實的掌握了網絡爬蟲方面的知識,認識到了自己的不足。在學習過程中會遇到很多不認識的庫也缺少對對庫中方法的應用這令我非常頭疼,但是通過各種途徑,認識到了加載庫和安裝各種環境的方法。這一次的作業使我對python的認識不再只停留在字面上,提升了個人的知識。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM