基于Python网络爬虫的美国GDP数据分析

一、选题的背景

从社会层面来说,分析美国的GDP数据,能够为我国的经济发展提供指导意义,达到趋利避害,学习美国的经验,以美国为鉴的目的。 从技术方面来说,爬虫是新时代新兴的技术,掌握爬虫,能不被社会所淘汰,紧跟时代的潮流, 从数据来源来说,GDP数据是比较好爬取的数据,不会出现因为恶意爬取而被封ip的情况,同时,GDP也是新时代衡量国家经济实力的标准。身为新时代大学生,更应该关心国家发展,国家的发展贡献自己的一份力。

二、主题式网络爬虫设计方案

1.主题式网络爬虫名称:

 基于Python网络爬虫的美国GDP数据分析

2.主题式网络爬虫爬取的内容与数据特征分析:

 爬取2010、2018年美国GDP信息,然后获取GDP、人均GDP、名义增速与州之间的排名关系

3. 主题式网络爬虫设计方案概述(包括实现思路与技术难点):

首先使用Python爬取美国GDP信息,然后对数据进行处理、清洗,去掉无需以及无效内容,然后对州与GDP、人均GDP、名义增速之间的关系进行分析,画出排名、GDP占比等图。然后画出线性回归方程。难点则在于如何避免报错,在对大量的数据进行处理的时候,如何处理避免报错也是难点所在。还有就是,如何准确的找到数据所在的标签也是一大难点。

三、主题页面的结构特征分析

1.主题页面的结构特征分析:

打开开发者工具,通过逐个查找找到需要数据的所在位置,发现所需要的数据都在<tbody>下的<tr>内的<td>中

2.Htmls 页面解析:

通过用request.get的方法爬取页面信息,再用BeautifulSoup解析,从而获得页面源代码。

3.节点(标签)查找方法与遍历方法 (必要时画出节点树结构):

    查找方法:find、select
    遍历方法:for循环

四、网络爬虫程序设计

 1.导入所需要的库

 1 import bs4
 2 import matplotlib.pyplot as plt
 3 import pandas as pd
 4 import requests
 5 from bs4 import BeautifulSoup
 6 from scipy.optimize import leastsq
 7 import csv
 8 import jieba
 9 import wordcloud
10 from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
11 import pyecharts
12 import numpy as np

2.爬取网站

1 url = 'https://www.phb123.com/city/GDP/36760.html'
2 f = open("GDP.csv",mode="w",encoding='utf-8')
3 r1 = requests.get(url)
4 r1.encoding = r1.apparent_encoding
5 soup1 = BeautifulSoup(r1.text, 'html.parser')
6 print(r1.text)

3.数据处理

 1 paiming,zhou,GDP,rjGDP,mingyi=[],[],[],[],[]
 2 for tr1 in soup1.find_all('tr'):
 3     if isinstance(tr1, bs4.element.Tag):
 4         tds = tr1('td')
 5         paiming.append(tds[0].text.strip())
 6         zhou.append(tds[1].text.strip())
 7         GDP.append(tds[2].text.strip())
 8         rjGDP.append(tds[3].text.strip())
 9         mingyi.append(tds[4].text.strip())
10 paiming=paiming[1:-1]
11 zhou=zhou[1:-1]
12 GDP=GDP[1:-1]
13 rjGDP=rjGDP[1:-1]
14 mingyi=mingyi[1:-1]
15 for i in range(len(paiming)):
16     df=pd.DataFrame({"排名":paiming,
17                 "":zhou,
18                 "GDP":GDP,
19                 "人均GDP":rjGDP,
20                 "名义增速":mingyi})
21 df.to_csv('GDP.csv',encoding='utf-8',index=False)
22 GDP= pd.read_csv('GDP.csv',encoding='utf-8')
23 
24 #print(df)

4.绘图

 1 x=[]
 2 y=[]
 3 r=[]
 4 t=[]
 5 for i in GDP[''][:15]:
 6     y.append(i)
 7 for j in GDP['GDP'][:15]:
 8     x.append(j)
 9 
10 for f in GDP['人均GDP'][:15]:
11     r.append(f)
12 
13 for u in GDP['名义增速'][:15]:
14     t.append(u)
15 #print("a:",t)
16 #print("c:",r)
17 #print("x:", x)
18 #print("y:", y)
19 plt.xlabel('GDP')
20 plt.ylabel('')
21 plt.title('2018年美国GDP排名前15的州')
22 bar_height=0.3
23 plt.rcParams["font.sans-serif"]=["SimHei"]
24 plt.barh(y,x,height=bar_height)
25 plt.show()

绘制州与州之间的GDP水平柱状图

1 plt.figure(figsize=(20,6))
2 plt.scatter(x[::-1],y[::-1],c='y',label='2018年全美经济前15的州折线图')
3 plt.plot(x,y,c='r')
4 plt.rcParams["font.sans-serif"]=["SimHei"]
5 plt.xlabel('')
6 plt.ylabel("GDP(亿元)")
7 plt.title('2018年全美各州GDP排名')
8 plt.legend(loc="upper left")
9 plt.show()

而折线图能体现出美国州之间的经济趋势

 1 x=GDP[''][:15]
 2 y=GDP['人均GDP'].values[:15]
 3 plt.xlabel('')
 4 plt.ylabel('人均GDP')
 5 plt.title('2018年美国人均GDP排名前15的州')
 6 bar_height=0.3
 7 plt.rcParams["font.sans-serif"]=["SimHei"]
 8 plt.barh(x,y,height=bar_height)
 9 plt.legend(loc=0)
10 plt.show()

绘制2018年人均GDP排名前15的州的水平柱状图

 1 x=GDP[''][:15][::-1]
 2 y=GDP['名义增速'].values[:15]
 3 plt.xlabel('')
 4 plt.ylabel('名义增速')
 5 plt.title('2018年美国名义增速排名前15的州')
 6 bar_height=0.3
 7 plt.rcParams["font.sans-serif"]=["SimHei"]
 8 plt.barh(x,y,height=bar_height)
 9 plt.legend(loc=0)
10 plt.show()

绘制2018年名义增速排名前15的州

爬取2010年美国GDP数据

 1 url1 = 'https://www.phb123.com/city/GDP/2412.html'
 2 f = open("2010GDP.csv",mode="w",encoding='utf-8')
 3 r2 = requests.get(url1)
 4 r2.encoding = r2.apparent_encoding
 5 soup1 = BeautifulSoup(r2.text, 'html.parser')
 6 paiming,zhou,GDP,rjGDP,mingyi=[],[],[],[],[]
 7 for tr1 in soup1.find_all('tr'):
 8     if isinstance(tr1, bs4.element.Tag):
 9         tds = tr1('td')
10         paiming.append(tds[0].text.strip())
11         zhou.append(tds[1].text.strip())
12         GDP.append(tds[2].text.strip())
13         rjGDP.append(tds[5].text.strip())
14 paiming=paiming[-1:1]
15 zhou=zhou[-1:1]
16 GDP=GDP[-1:1]
17 rjGDP=rjGDP[-1:1]
18 for i in range(len(paiming)):
19     df=pd.DataFrame({"排名":paiming,
20                 "":zhou,
21                 "GDP":GDP,
22                 "人均GDP":rjGDP,})
23 df.to_csv('2010GDP.csv',encoding='utf-8',index=False)
24 GDP = pd.read_csv('2010GDP.csv',encoding='utf-8')
25 #print(df)
26 a=[]
27 b=[]
28 c=[]
29 for q in GDP[''][:15]:
30     a.append(q)
31 for w in GDP['GDP'][:15]:
32     b.append(w)
33 for y in GDP['人均GDP'][:15]:
34     c.append(y)
35 print("d:",c)
36 #print("a:",a)
37 #print("b:",b)
38 x=GDP[''][:15][::-1]
39 y=GDP['人均GDP'].values[:15]
40 plt.xlabel('GDP')
41 plt.ylabel('')
42 plt.title('2010年美国GDP排名前15的州')
43 bar_height=0.3
44 plt.rcParams["font.sans-serif"]=["SimHei"]
45 plt.barh(a,b,height=bar_height)
46 plt.show()

绘制2010年美国GDP排名前15的州

1 plt.xlabel('')
2 plt.ylabel('人均GDP')
3 plt.title('2010年美国名义增速排名前15的州')
4 bar_height=0.3
5 plt.rcParams["font.sans-serif"]=["SimHei"]
6 plt.barh(x,y,height=bar_height)
7 plt.legend(loc=0)
8 plt.show()

绘制2010年美国名义增速排名前15的州

 1 x=GDP[''][:15]
 2 y=GDP['GDP'].values[:15]
 3 plt.figure(figsize=(30, 30))  # 做出数据可视化分析
 4 name = np.array(x)
 5 rs = np.array(y)
 6 explode = np.zeros(len(rs))
 7 plt.legend(loc='center right')
 8 plt.title('2018年各州GDP占比')
 9 patches, l_text, p_text = plt.pie(rs,
10                                   explode=explode,
11                                   labels=name,
12                                   autopct='%1.1f%%',
13                                   shadow=False,
14                                   pctdistance=0.6,
15                                   labeldistance=1.1,
16                                   startangle=180,
17                                   radius=1.2,
18                                   counterclock=False,
19                                   textprops={'fontsize': 10, 'color': 'black'}
20                                   )
21 
22 plt.legend(y)
23 for t in p_text:
24     t.set_size(10)
25 
26 for t in l_text:
27     t.set_size(10)
28 
29 plt.rcParams['font.sans-serif'] = ['SimHei']
30 plt.axis('equal')
31 plt.show()

画出2018年美国各州GDP占比图

 1 x=GDP[''][:15]
 2 y=GDP['人均GDP'].values[:15]
 3 plt.figure(figsize=(30, 30))  # 做出数据可视化分析
 4 name = np.array(x)
 5 rs = np.array(y)
 6 explode = np.zeros(len(rs))
 7 plt.legend(loc='center right')
 8 plt.title('2018年各州人均GDP占比')
 9 patches, l_text, p_text = plt.pie(rs,
10                                   explode=explode,
11                                   labels=name,
12                                   autopct='%1.1f%%',
13                                   shadow=False,
14                                   pctdistance=0.6,
15                                   labeldistance=1.1,
16                                   startangle=180,
17                                   radius=1.2,
18                                   counterclock=False,
19                                   textprops={'fontsize': 10, 'color': 'black'}
20                                   )
21 
22 plt.legend(y)
23 for t in p_text:
24     t.set_size(10)
25 
26 for t in l_text:
27     t.set_size(10)
28 
29 plt.rcParams['font.sans-serif'] = ['SimHei']
30 plt.axis('equal')
31 plt.show()

画出2018年各州人均GDP占比图


绘制排名和人均GDP的拟合曲线

 1 def func(params, x):
 2     a, b, c = params
 3     return a * x * x + b * x + c
 4 
 5 def error(params, x, y):
 6     return func(params, x) - y
 7 
 8 px = GDP['排名'].values
 9 py = GDP['人均GDP'].values
10 params0 = [5, 2, 10]
11 Para = leastsq(error, params0, args=(px, py))
12 a, b, c = Para[0]
13 plt.figure(figsize=(20, 8))
14 plt.tick_params(labelsize=11)
15 plt.scatter(px, py, color='green', label="样本点")
16 x = GDP['排名'].values
17 y = a * x * x + b * x + c
18 plt.plot(x, y, color='red', label='拟合曲线')
19 plt.xlabel('排名')
20 plt.ylabel('人均GDP')
21 plt.title('排名和人均GDP的拟合曲线')
22 plt.legend()
23 plt.grid(True, linestyle='--',
24          color='g', linewidth='0.5')
25 plt.rcParams['font.sans-serif'] = ['SimHei']
26 plt.show()

绘制排名和人均GDP的拟合曲线

 1 def func(params, x):
 2     a, b, c = params
 3     return a * x * x + b * x + c
 4 
 5 def error(params, x, y):
 6     return func(params, x) - y
 7 
 8 px = GDP['排名'].values
 9 py = GDP['GDP'].values
10 params0 = [5, 2, 10]
11 Para = leastsq(error, params0, args=(px, py))
12 a, b, c = Para[0]
13 plt.figure(figsize=(20, 8))
14 plt.tick_params(labelsize=11)
15 plt.scatter(px, py, color='green', label="样本点")
16 x = GDP['排名'].values
17 y = a * x * x + b * x + c
18 plt.plot(x, y, color='red', label='拟合曲线')
19 plt.xlabel('排名')
20 plt.ylabel('GDP')
21 plt.title('2018年美国州和GDP的拟合曲线')
22 plt.legend()
23 plt.grid(True, linestyle='--',
24          color='g', linewidth='0.5')
25 plt.rcParams['font.sans-serif'] = ['SimHei']
26 plt.show()

绘制排名和人均GDP的拟合曲线

 1 GDP1= pd.read_csv('2010GDP.csv',encoding='utf-8')
 2 def func(params, x):
 3     a, b, c = params
 4     return a * x * x + b * x + c
 5 
 6 def error(params, x, y):
 7     return func(params, x) - y
 8 
 9 px = GDP1['排名'].values
10 py = GDP1['GDP'].values
11 params0 = [5, 2, 10]
12 Para = leastsq(error, params0, args=(px, py))
13 a, b, c = Para[0]
14 plt.figure(figsize=(20, 8))
15 plt.tick_params(labelsize=11)
16 plt.scatter(px, py, color='green', label="样本点")
17 x = GDP1['排名'].values
18 y = a * x * x + b * x + c
19 plt.plot(x, y, color='red', label='拟合曲线')
20 plt.xlabel('排名')
21 plt.ylabel('GDP')
22 plt.title('2010美国州和GDP的拟合曲线')
23 plt.legend()
24 plt.grid(True, linestyle='--',
25          color='g', linewidth='0.5')
26 plt.rcParams['font.sans-serif'] = ['SimHei']
27 plt.show()

将以上各部分的代码汇总,附上完整程序代码

  1 import bs4
  2 import matplotlib.pyplot as plt
  3 import pandas as pd
  4 import requests
  5 from bs4 import BeautifulSoup
  6 from scipy.optimize import leastsq
  7 import csv
  8 import jieba
  9 import wordcloud
 10 from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
 11 import pyecharts
 12 import numpy as np
 13 url = 'https://www.phb123.com/city/GDP/36760.html'
 14 f = open("GDP.csv",mode="w",encoding='utf-8')
 15 r1 = requests.get(url)
 16 r1.encoding = r1.apparent_encoding
 17 soup1 = BeautifulSoup(r1.text, 'html.parser')
 18 print(r1.text)
 19 paiming,zhou,GDP,rjGDP,mingyi=[],[],[],[],[]
 20 for tr1 in soup1.find_all('tr'):
 21     if isinstance(tr1, bs4.element.Tag):
 22         tds = tr1('td')
 23         paiming.append(tds[0].text.strip())
 24         zhou.append(tds[1].text.strip())
 25         GDP.append(tds[2].text.strip())
 26         rjGDP.append(tds[3].text.strip())
 27         mingyi.append(tds[4].text.strip())
 28 paiming=paiming[1:-1]
 29 zhou=zhou[1:-1]
 30 GDP=GDP[1:-1]
 31 rjGDP=rjGDP[1:-1]
 32 mingyi=mingyi[1:-1]
 33 for i in range(len(paiming)):
 34     df=pd.DataFrame({"排名":paiming,
 35                 "":zhou,
 36                 "GDP":GDP,
 37                 "人均GDP":rjGDP,
 38                 "名义增速":mingyi})
 39 df.to_csv('GDP.csv',encoding='utf-8',index=False)
 40 GDP= pd.read_csv('GDP.csv',encoding='utf-8')
 41 
 42 #print(df)
 43 x=[]
 44 y=[]
 45 r=[]
 46 t=[]
 47 for i in GDP[''][:15]:
 48     y.append(i)
 49 for j in GDP['GDP'][:15]:
 50     x.append(j)
 51 
 52 for f in GDP['人均GDP'][:15]:
 53     r.append(f)
 54 
 55 for u in GDP['名义增速'][:15]:
 56     t.append(u)
 57 #print("a:",t)
 58 #print("c:",r)
 59 #print("x:", x)
 60 #print("y:", y)
 61 plt.xlabel('GDP')
 62 plt.ylabel('')
 63 plt.title('2018年美国GDP排名前15的州')
 64 bar_height=0.3
 65 plt.rcParams["font.sans-serif"]=["SimHei"]
 66 plt.barh(y,x,height=bar_height)
 67 plt.show()
 68 plt.figure(figsize=(20,6))
 69 plt.scatter(x[::-1],y[::-1],c='y',label='2018年全美经济前15的州折线图')
 70 plt.plot(x,y,c='r')
 71 plt.rcParams["font.sans-serif"]=["SimHei"]
 72 plt.xlabel('')
 73 plt.ylabel("GDP(亿元)")
 74 plt.title('2018年全美各州GDP排名')
 75 plt.legend(loc="upper left")
 76 plt.show()
 77 x=GDP[''][:15]
 78 y=GDP['人均GDP'].values[:15]
 79 plt.xlabel('')
 80 plt.ylabel('人均GDP')
 81 plt.title('2018年美国人均GDP排名前15的州')
 82 bar_height=0.3
 83 plt.rcParams["font.sans-serif"]=["SimHei"]
 84 plt.barh(x,y,height=bar_height)
 85 plt.legend(loc=0)
 86 plt.show()
 87 x=GDP[''][:15][::-1]
 88 y=GDP['名义增速'].values[:15]
 89 plt.xlabel('')
 90 plt.ylabel('名义增速')
 91 plt.title('2018年美国名义增速排名前15的州')
 92 bar_height=0.3
 93 plt.rcParams["font.sans-serif"]=["SimHei"]
 94 plt.barh(x,y,height=bar_height)
 95 plt.legend(loc=0)
 96 plt.show()
 97 url1 = 'https://www.phb123.com/city/GDP/2412.html'
 98 f = open("2010GDP.csv",mode="w",encoding='utf-8')
 99 r2 = requests.get(url1)
100 r2.encoding = r2.apparent_encoding
101 soup1 = BeautifulSoup(r2.text, 'html.parser')
102 paiming,zhou,GDP,rjGDP,mingyi=[],[],[],[],[]
103 for tr1 in soup1.find_all('tr'):
104     if isinstance(tr1, bs4.element.Tag):
105         tds = tr1('td')
106         paiming.append(tds[0].text.strip())
107         zhou.append(tds[1].text.strip())
108         GDP.append(tds[2].text.strip())
109         rjGDP.append(tds[5].text.strip())
110 paiming=paiming[-1:1]
111 zhou=zhou[-1:1]
112 GDP=GDP[-1:1]
113 rjGDP=rjGDP[-1:1]
114 for i in range(len(paiming)):
115     df=pd.DataFrame({"排名":paiming,
116                 "":zhou,
117                 "GDP":GDP,
118                 "人均GDP":rjGDP,})
119 df.to_csv('2010GDP.csv',encoding='utf-8',index=False)
120 GDP = pd.read_csv('2010GDP.csv',encoding='utf-8')
121 #print(df)
122 a=[]
123 b=[]
124 c=[]
125 for q in GDP[''][:15]:
126     a.append(q)
127 for w in GDP['GDP'][:15]:
128     b.append(w)
129 for y in GDP['人均GDP'][:15]:
130     c.append(y)
131 print("d:",c)
132 #print("a:",a)
133 #print("b:",b)
134 x=GDP[''][:15][::-1]
135 y=GDP['人均GDP'].values[:15]
136 plt.xlabel('GDP')
137 plt.ylabel('')
138 plt.title('2010年美国GDP排名前15的州')
139 bar_height=0.3
140 plt.rcParams["font.sans-serif"]=["SimHei"]
141 plt.barh(a,b,height=bar_height)
142 plt.show()
143 plt.xlabel('')
144 plt.ylabel('人均GDP')
145 plt.title('2010年美国名义增速排名前15的州')
146 bar_height=0.3
147 plt.rcParams["font.sans-serif"]=["SimHei"]
148 plt.barh(x,y,height=bar_height)
149 plt.legend(loc=0)
150 plt.show()
151 x=GDP[''][:15]
152 y=GDP['GDP'].values[:15]
153 plt.figure(figsize=(30, 30))  # 做出数据可视化分析
154 name = np.array(x)
155 rs = np.array(y)
156 explode = np.zeros(len(rs))
157 plt.legend(loc='center right')
158 plt.title('2018年各州GDP占比')
159 patches, l_text, p_text = plt.pie(rs,
160                                   explode=explode,
161                                   labels=name,
162                                   autopct='%1.1f%%',
163                                   shadow=False,
164                                   pctdistance=0.6,
165                                   labeldistance=1.1,
166                                   startangle=180,
167                                   radius=1.2,
168                                   counterclock=False,
169                                   textprops={'fontsize': 10, 'color': 'black'}
170                                   )
171 
172 plt.legend(y)
173 for t in p_text:
174     t.set_size(10)
175 
176 for t in l_text:
177     t.set_size(10)
178 
179 plt.rcParams['font.sans-serif'] = ['SimHei']
180 plt.axis('equal')
181 plt.show()
182 x=GDP[''][:15]
183 y=GDP['人均GDP'].values[:15]
184 plt.figure(figsize=(30, 30))  # 做出数据可视化分析
185 name = np.array(x)
186 rs = np.array(y)
187 explode = np.zeros(len(rs))
188 plt.legend(loc='center right')
189 plt.title('2018年各州人均GDP占比')
190 patches, l_text, p_text = plt.pie(rs,
191                                   explode=explode,
192                                   labels=name,
193                                   autopct='%1.1f%%',
194                                   shadow=False,
195                                   pctdistance=0.6,
196                                   labeldistance=1.1,
197                                   startangle=180,
198                                   radius=1.2,
199                                   counterclock=False,
200                                   textprops={'fontsize': 10, 'color': 'black'}
201                                   )
202 
203 plt.legend(y)
204 for t in p_text:
205     t.set_size(10)
206 
207 for t in l_text:
208     t.set_size(10)
209 
210 plt.rcParams['font.sans-serif'] = ['SimHei']
211 plt.axis('equal')
212 plt.show()
213 def func(params, x):
214     a, b, c = params
215     return a * x * x + b * x + c
216 
217 def error(params, x, y):
218     return func(params, x) - y
219 
220 px = GDP['排名'].values
221 py = GDP['人均GDP'].values
222 params0 = [5, 2, 10]
223 Para = leastsq(error, params0, args=(px, py))
224 a, b, c = Para[0]
225 plt.figure(figsize=(20, 8))
226 plt.tick_params(labelsize=11)
227 plt.scatter(px, py, color='green', label="样本点")
228 x = GDP['排名'].values
229 y = a * x * x + b * x + c
230 plt.plot(x, y, color='red', label='拟合曲线')
231 plt.xlabel('排名')
232 plt.ylabel('人均GDP')
233 plt.title('排名和人均GDP的拟合曲线')
234 plt.legend()
235 plt.grid(True, linestyle='--',
236          color='g', linewidth='0.5')
237 plt.rcParams['font.sans-serif'] = ['SimHei']
238 plt.show()
239 def func(params, x):
240     a, b, c = params
241     return a * x * x + b * x + c
242 
243 def error(params, x, y):
244     return func(params, x) - y
245 
246 px = GDP['排名'].values
247 py = GDP['GDP'].values
248 params0 = [5, 2, 10]
249 Para = leastsq(error, params0, args=(px, py))
250 a, b, c = Para[0]
251 plt.figure(figsize=(20, 8))
252 plt.tick_params(labelsize=11)
253 plt.scatter(px, py, color='green', label="样本点")
254 x = GDP['排名'].values
255 y = a * x * x + b * x + c
256 plt.plot(x, y, color='red', label='拟合曲线')
257 plt.xlabel('排名')
258 plt.ylabel('GDP')
259 plt.title('2018年美国州和GDP的拟合曲线')
260 plt.legend()
261 plt.grid(True, linestyle='--',
262          color='g', linewidth='0.5')
263 plt.rcParams['font.sans-serif'] = ['SimHei']
264 plt.show()
265 GDP1= pd.read_csv('2010GDP.csv',encoding='utf-8')
266 def func(params, x):
267     a, b, c = params
268     return a * x * x + b * x + c
269 
270 def error(params, x, y):
271     return func(params, x) - y
272 
273 px = GDP1['排名'].values
274 py = GDP1['GDP'].values
275 params0 = [5, 2, 10]
276 Para = leastsq(error, params0, args=(px, py))
277 a, b, c = Para[0]
278 plt.figure(figsize=(20, 8))
279 plt.tick_params(labelsize=11)
280 plt.scatter(px, py, color='green', label="样本点")
281 x = GDP1['排名'].values
282 y = a * x * x + b * x + c
283 plt.plot(x, y, color='red', label='拟合曲线')
284 plt.xlabel('排名')
285 plt.ylabel('GDP')
286 plt.title('2010美国州和GDP的拟合曲线')
287 plt.legend()
288 plt.grid(True, linestyle='--',
289          color='g', linewidth='0.5')
290 plt.rcParams['font.sans-serif'] = ['SimHei']
291 plt.show()

五、总结

美国的GDP总量仍处于世界第一的位置,且人均GDP也处于世界前列,我国与美国仍有差距。但美国的GDP增速落后于中国,中国赶超美国指日可待。达到了预期的目标:分析了美国的GDP数据信息,给我国的经济发展提供指导意义。

通过本次课程设计,学会了爬虫的基本原理和操作,数据清洗和处理的方式,以及request的方法。在爬取数据时通过索引位置进行页面切换,实现翻页,再用bs4  来清洗数据,再用Pecharts和matplotlib进行绘图。

获取数据并处理后就是进行绘图,首先柱状图,先给x,y赋值,然后用matplotlib插件绘图

然后就是在进行数据分析时,去除掉网页无效数据,重复数据行,这种的会影响最终的判断。

改进与建议:在进行线性回归时,应该考虑到不同年份对GDP的影响。因为2008年爆发的金融危机使美国经济受到重创,不能因此而断定美国的GDP走向。因此要加入一些变量进行分析。

posted @ 2021-12-29 22:41  何佳辉  阅读(488)  评论(0编辑  收藏  举报