利用Python爬取github上commits信息

简介

前段时间,帮同学做了一个爬虫的作业,比较基础的那种,这里简单记录一下吧。要爬取的内容就是github上commits在1200次以上的人及其commits分布情况。完整代码下载地址:https://pan.baidu.com/s/1BUE5WqrCQ4PDEsS8aBX7Vg

准备

  1. 使用Python3.6
  2. 使用requests和BeautifulSoup库(不会用的可以看一下这两个中文文档requestsBeautifulSoup)

抓取用户个人页面

查看网址可以发现github用户页面网址格式都是"https://github.com/" + 用户ID,用户的几个页面格式是"https://github.com/" + 用户ID + “?tab=…”,这样我们就可以从一个用户开始,根据Followers和Following关系,爬取到大量的用户信息,我这里采用BFS(广度优先搜索)的思想,从一个用户ID为"u2"的用户开始,一层层的爬取用户的Following名单,并存到用户列表里备选。
代码如下:

#获取当前用户关注者列表下的用户名,放进备选用户列表里,并返回队尾
#函数参数为用户个人界面的url和队尾序号
def get_user_following(userFolloingUrl, userListTail):    
    req = requests.get(userFollowingUrl)
    html = req.text
    soup = BeautifulSoup(html,"lxml")
    userFollowings = soup.find_all('span', class_ = 'f4 link-gray-dark')
    for element in userFollowings:
        userName = element.text.replace('\xa0'*8,'\n\n') 
        if userName != "":
            userList.append(userName)
            userListTail = userListTail + 1
    return userListTail

获取commits信息

通过观察网址特点可以发现,每个用户每个月的提交信息网页url为:“https://github.com/” + 用户名 + “?tab=overview&from=” + 起始日期+"&to=" + 结束日期,以此特点遍历每个备选用户近七年每年十二个月的提交信息,确定其commits数量是否超过1200次。
代码如下:

while userListHead < userListTail:
    if userDataNum >= 20 :
        break
    userName = userList[userListHead]
    userListHead = userListHead + 1
    userFollowingUrl = "https://github.com/"+userName+"?tab=following"
    userListTail = get_user_following(userFollowingUrl, userListTail)
    time.sleep(int(random.uniform(2,4)))
    commitsNum = 0
    
    print("Get the user's following successfully")
   
    #遍历该用户最近七年每个月的commits记录
    for year in ["2018","2017","2016","2015","2014","2013","2012"]:
        for month in range(12,0,-1):
            eachMonthCommitsUrl = "https://github.com/"+userName+"?tab=overview&from="+year+startDate[month]+"&to="+year+endDate[month]
            time.sleep(int(
当然可以!以下是一个使用Python爬取GitHub源码的脚本示例。这个脚本使用了`requests`和`BeautifulSoup`库来获取和解析GitHub页面上的源码链接,并使用`git`库来克隆仓库。 首先,确保你已经安装了所需的库: ```bash pip install requests beautifulsoup4 GitPython ``` 然后,创建一个Python脚本文件(例如`github_scraper.py`),并添加以下代码: ```python import requests from bs4 import BeautifulSoup from git import Repo import os def get_github_repo_links(user): url = f'https://github.com/{user}?tab=repositories' response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') repo_links = [] for repo in soup.find_all('a', {'itemprop': 'name codeRepository'}): repo_links.append('https://github.com' + repo['href']) return repo_links def clone_repos(repo_links, destination): if not os.path.exists(destination): os.makedirs(destination) for repo_link in repo_links: repo_name = repo_link.split('/')[-1] clone_path = os.path.join(destination, repo_name) if not os.path.exists(clone_path): print(f'Cloning {repo_link} into {clone_path}') Repo.clone_from(repo_link, clone_path) else: print(f'{repo_name} already exists in {destination}') if __name__ == '__main__': github_user = 'your_github_username' repos_destination = 'repos' repo_links = get_github_repo_links(github_user) clone_repos(repo_links, repos_destination) ``` 这个脚本的工作流程如下: 1. `get_github_repo_links`函数获取指定GitHub用户的所有仓库链接。 2. `clone_repos`函数将这些仓库克隆到本地指定目录。 请将`your_github_username`替换为你要爬取GitHub用户名。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值