用 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP