用 Python 在 GitHub 上獲取使用者的排名前十的收藏庫?


Git 是最流行的版本控制系統,數百萬的開發人員利用 Git 管理其專案或檔案(程式碼)。在本文中,我們將嘗試獲取一個月內排名前十的、被收藏次數最多的程式碼庫。

由於我們主要抓取的是 GitHub 程式碼庫,因此我們將主要使用

Requests 和 BeautifulSoup 庫來獲取程式碼庫。

我們將結果儲存在檔案中並將其顯示出來。它將根據位置(收藏次數)顯示名稱和程式碼庫的排名。

以下是實現此功能的程式碼

import requests
from bs4 import BeautifulSoup
r = requests.get('https://github.com/trending/lua?since=monthly')
bs = BeautifulSoup(r.text, 'lxml')
lista_repo = bs.find_all('ol', class_='repo-list')
f1 = open('starred-repos.txt', 'w')
for lr in lista_repo:
   aux = lr.find_all('div', class_='d-inline-block col-9 mb-1')
   for ld in aux:
      rank = ld.find_all('a')
      f1.writelines(str(rank))
      f1.writelines('\n')
f1.close()
f1 = open('starred-repos.txt', 'r')
texto = []
for x in f1:
   if x[0] == '[' and x[1] == '<' and x[2] == 'a':
      na = x.split('"')
      texto.append(na[1])
f1.close()
f1 = open('starred-repos.txt', 'w')
f1.writelines('{}\t {}\t\t {}\t\n\n'.format('Position ', 'Name ', 'Repositories '))
for i in range(10):
   tex= texto[i].split('/')
   name = tex[1]
   repos = tex[2]
   f1.writelines('{}- \t {}\t\t {}'.format(i + 1, name, repos))
   f1.writelines('\n')
f1.close()
f1 = open('starred-repos.txt', 'r')
print(f1.read())
f1.close()

輸出

Position            Name           Repositories

1-              skywind3000           z.lua
2-                  Kong               kong
3-                 Gawen              WireHub
4-              PapyElGringo      material-awesome
5-                koreader           koreader
6-                stijnwop       guidanceSteering
7-               Courseplay         courseplay
8-                Tencent            LuaPanda
9-                 ntop               ntopng
10-             awesomeWM             awesome

更新時間:2019 年 7 月 30 日

96 次瀏覽

開啟你的 職業

完成課程並獲得認證

開始學習
廣告
© . All rights reserved.