python程序設計作品,希望對您有幫助,希望您的一鍵三連!
程序設計報告
1.爬取數(shù)據(jù)的意義
富豪榜的出現(xiàn),體現(xiàn)了人們思想的變化:由保守藏富向正向面對財富的轉變;由保守向文明開放(–說明了大眾媒體的進步與教育的普及等思想工具的極大地提高);標志著人們對財富對經(jīng)濟正在走向新紀元。
2.程序詳細設計
(1)設計思路流程圖:
(2)設計代碼實現(xiàn):
①導入相關數(shù)據(jù)庫
②獲取網(wǎng)頁的url,并模擬向網(wǎng)頁放出請求,并獲取響應。按f12進入控制臺獲取頁面的代碼,使用正則提取進行具體代碼塊的尋找,并把對應的代碼放入到集合中,方便下面存儲時候使用數(shù)據(jù)。
③創(chuàng)建一個新的工作表,命名相關的屬性,并將上述集合中的數(shù)據(jù)導入其中
④運行檢驗已經(jīng)可以生成盛放信息的excel表格,下一步實現(xiàn)數(shù)據(jù)篩選和可視化處理
⑤創(chuàng)建函數(shù),對富豪前十名和富豪所在國家進行數(shù)據(jù)統(tǒng)計,然后分別畫出餅狀圖和條形圖
⑥通過條形圖和餅狀圖實現(xiàn)數(shù)據(jù)的客觀的可視化
表 1餅狀圖
表 2 條形圖文章來源:http://www.zghlxwxcb.cn/news/detail-504414.html
3.報告總結
企業(yè)財富的增長,源于國家經(jīng)濟的發(fā)展。可以相信,中國福布斯上榜人數(shù)很快就會超過美國,隨著中美兩國經(jīng)濟規(guī)模差距縮小,中美富豪榜財富總額差距也會逐步縮小,直至逆轉.中美之間的博弈將會長期存在,但中國超越美國成為世界第一大經(jīng)濟體的趨勢不可阻擋,中國只不過是時隔100多年重新回到她原來的位置;我們個人的財富命運與國運(國家發(fā)展)是息息相關的,沒有一個人能夠超脫于國家大的發(fā)展背景而能超然存在。未來十年中國財富增長的方式主要通過資本市場體現(xiàn),投資中國會成為主旋律,期待“富豪為廣大的股民打工”早日到來。文章來源地址http://www.zghlxwxcb.cn/news/detail-504414.html
代碼
from bs4 import BeautifulSoup
import requests
import xlwt
import xlrd
import pandas as ps
import numpy
import matplotlib.pyplot as plt
## 讀取所有福布斯排行榜數(shù)據(jù)
def loadalldata():
alldata = []
for i in range(1,16,1):
url = "https://www.phb123.com/renwu/fuhao/shishi_"+str(i)+".html"
data = loaddata(url)
alldata = alldata + data
return alldata
## 將爬取的數(shù)據(jù)保存到文件
def savedata(path,persionlist):
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('test')
worksheet.write(0, 0, '排名')
worksheet.write(0, 1, '姓名')
worksheet.write(0, 2, '財富')
worksheet.write(0, 3, '企業(yè)')
worksheet.write(0, 4, '國家')
for i in range(1,len(persionlist)+1,1):
worksheet.write(i, 0, persionlist[i-1]['num'])
worksheet.write(i, 1, persionlist[i-1]['name'])
worksheet.write(i, 2, persionlist[i-1]['money'])
worksheet.write(i, 3, persionlist[i-1]['company'])
worksheet.write(i, 4, persionlist[i-1]['country'])
workbook.save(path)
print("數(shù)據(jù)保存成功:"+path)
## 讀取網(wǎng)站數(shù)據(jù)
def loaddata(url):
headers = {
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/72.0.3626.121 Safari/537.36'
}
f = requests.get(url,headers=headers) #Get該網(wǎng)頁從而獲取該html內(nèi)容
soup = BeautifulSoup(f.content, "lxml") #用lxml解析器解析該網(wǎng)頁的內(nèi)容, 好像f.text也是返回的html
# print(f.content.decode()) #嘗試打印出網(wǎng)頁內(nèi)容,看是否獲取成功
ranktable = soup.find_all('table',class_="rank-table" )[0] #獲取排行榜表格
trlist = ranktable.find_all('tr') #獲取表格中所有tr標簽
trlist.pop(0) #去掉第一個元素
persionlist = []
for tr in trlist:
persion = {}
persion['num'] = tr.find_all('td')[0].string #編號
persion['name'] = tr.find_all('td')[1].p.string #名稱
persion['money'] = tr.find_all('td')[2].string #財產(chǎn)
persion['company'] = tr.find_all('td')[3].string #企業(yè)
persion['country'] = tr.find_all('td')[4].a.string #國家
persionlist.append(persion)
print("頁面"+url+"爬取成功")
return persionlist
## 取出排行榜前十的姓名和財富數(shù)據(jù) 以兩個list返回
def loadtop10(path):
book = xlrd.open_workbook(path)
sheet1 = book.sheets()[0]
namelist = sheet1.col_values(1)
moneylist = sheet1.col_values(2)
namelist = namelist[1:11]
moneylist = moneylist[1:11]
moneylist2 = []
for a in moneylist:
a = int(a[0:-3])
moneylist2.append(a)
print("取出排行榜前十的姓名和財富數(shù)據(jù)")
print(namelist)
print(moneylist2)
return namelist,moneylist2
## 統(tǒng)計排行榜中每個國家的上榜人數(shù) 以字典list返回
def countcountrynum(path):
book = xlrd.open_workbook(path)
sheet1 = book.sheets()[0]
countrylist = sheet1.col_values(4)[1:-1]
print(countrylist)
countryset = list(set(countrylist))
dictlist = []
for country in countryset:
obj = {"name":country,"count":0}
dictlist.append(obj)
## 統(tǒng)計出每個國家對應的數(shù)量
for obj in dictlist:
for a in countrylist:
if obj['name'] == a:
obj['count'] = obj['count'] + 1
print(dictlist)
## 將dictlist排序 數(shù)量多的放前面 8 5 6 9 3 2 4
for i in range(0,len(dictlist),1):
for j in range(0,len(dictlist)-i-1,1):
if dictlist[j]['count'] < dictlist[j+1]['count']:
temp = dictlist[j]
dictlist[j] = dictlist[j+1]
dictlist[j+1] = temp
dictlist2 = dictlist[0:5]
set2 = []
for a in dictlist2:
set2.append(a['name'])
othercount = 0;
for a in dictlist:
if a['name'] not in set2:
othercount = othercount + 1
dictlist2.append({"name":"其他","count":othercount})
print('獲取排行榜中每個國家的上榜人數(shù)')
print(dictlist2)
return dictlist2
## 繪制條形圖和餅狀圖
def drow():
plt.rcParams['font.sans-serif'] = ['SimHei'] # 設置中文字體
plt.figure('福布斯前十榜',figsize=(15,5))
## 讀取福布斯排行榜前十的數(shù)據(jù)
listx,listy = loadtop10('rank.xls')
plt.title('福布斯前十榜', fontsize=16)
plt.xlabel('人物', fontsize=14)
plt.ylabel('金額/億美元', fontsize=14)
plt.tick_params(labelsize=10)
plt.grid(linestyle=':', axis='y')
a = plt.bar(listx, listy, color='dodgerblue', label='Apple', align='center')
# 設置標簽
for i in a:
h = i.get_height()
plt.text(i.get_x() + i.get_width() / 2, h, '%d' % int(h), ha='center', va='bottom')
## -------------------------------------------------------------------------
dictlist = countcountrynum("fuhao.xls")
plt.figure('各國家上榜人數(shù)所占比例')
labels = []
sizes = []
for a in dictlist:
labels.append(a['name'])
sizes.append(a['count'])
explode = (0.1, 0, 0, 0, 0, 0)
plt.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', shadow=False, startangle=150)
plt.title("各國家上榜人數(shù)所占比例", fontsize=16)
plt.axis('equal') # 該行代碼使餅圖長寬相等
plt.show()
if __name__ == '__main__':
## 爬取數(shù)據(jù)
data = loadalldata()
## 保存數(shù)據(jù)
savedata("fuhao.xls",data) # py文件同級目錄創(chuàng)建rank.xls文件
## 展示數(shù)據(jù)
drow()
到了這里,關于Python程序設計期末作品完整版|代碼和程序設計文檔的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!