申明:資料來源于網路及書本,通過理解、實踐、整理成學習筆記,
文章目錄
- 完整代碼
- 運行結果截圖
完整代碼
import requests, configparser, os
class CSDNGetAuthorRank:
def get_author_rank(self):
__conf = configparser.ConfigParser()
if os.path.exists('../conf/csdn.ini'):
__conf.read('../conf/csdn.ini')
else:
__conf.read('conf/csdn.ini')
headers = {
__conf.get('get_author_rank_headers', 'headers'): __conf.get('get_author_rank_headers', 'headers_value')}
all_rank_list = []
for k in range(1, len(__conf.options('get_author_rank')) + 1):
url = __conf.get('get_author_rank', 'url_{}'.format(k))
res = requests.get(url, headers=headers)
res.encoding = res.apparent_encoding
all_rank_list_item = res.json()['data']['allRankListItem']
for i in all_rank_list_item:
rank_list = []
# 排名
current_rank = i['currentRank']
rank_list.append(current_rank)
# 用戶名
user_name = i['userName']
rank_list.append(user_name)
# 昵稱
nick_name = i['nickName']
rank_list.append(nick_name)
# 粉絲數
fans_count = str(i['fansCount'])
rank_list.append(fans_count)
# 獲贊
digg_count = str(i['diggCount'])
rank_list.append(digg_count)
# 博客等級
level = str(i['level'])
rank_list.append(level)
# 綜合指標
hot_rank_score = str(i['hotRankScore'])
rank_list.append(hot_rank_score)
# vip
vip = i['vip']
rank_list.append(vip)
# 在線
login_user_is_follow = i['loginUserIsFollow']
rank_list.append(login_user_is_follow)
all_rank_list.append(tuple(rank_list))
print(all_rank_list)
if os.path.exists('../results/excel/CSDN作者總榜排行前100.xls'):
csdn_author_data_xls = '../results/excel/CSDN作者總榜排行前100.xls'
else:
csdn_author_data_xls = 'results/excel/CSDN作者總榜排行前100.xls'
# 將獲取的CSDN資料寫入到excel檔案中
output = open(csdn_author_data_xls, 'w', encoding='gbk')
output.write('排名\t用戶名\t昵稱\t粉絲數\t獲贊\t博客等級\t綜合指標\t會員\t在線\n')
for i in range(len(all_rank_list)):
for j in range(len(all_rank_list[i])):
# write函式不能寫int型別的引數,所以使用str()轉化
output.write(str(all_rank_list[i][j]))
# 相當于Tab一下,換一個單元格
output.write('\t')
# 寫完一行立馬換行
output.write('\n')
output.close()
if __name__ == '__main__':
CSDNGetAuthorRank().get_author_rank()
運行結果截圖

推薦一:爬取天天基金的7663個基金排名保存到excel表
推薦二:爬取嗶哩嗶哩主播的頭像以昵稱命名保存到本地檔案
推薦三:爬取CSDN作者總榜排名保存到excel表
一個堅持學習,堅持成長,堅持分享的人,即使再不聰明,也一定會成為優秀的人!
如果看完覺得有所識訓的話,記得一鍵三連哦,謝謝大家!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/277335.html
標籤:python
上一篇:【python自動化辦公】電腦全盤路徑/指定路徑下檔案(夾)查找,并封裝為可執行程式
下一篇:智能垃圾分類
