Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Lấy top 10 repository được star nhiều nhất của người dùng trên GitHub bằng Python

Git là hệ thống kiểm soát phiên bản phổ biến nhất, nơi hàng triệu nhà phát triển quản lý dự án hoặc tệp (mã nguồn). Ở đây chúng ta sẽ thử lấy top 10 repository được star nhiều nhất trong một tháng.

Vì chúng ta chủ yếu cào dữ liệu repository GitHub, chúng ta sẽ sử dụng chủ yếu thư viện:

Requests và BeautifulSoup để lấy các repository.

Chúng ta sẽ lưu kết quả vào file và hiển thị. Kết quả sẽ hiển thị dựa trên vị trí (số star) kèm tên và repository.

Mã triển khai

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()

Kết quả

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