PyGithub的实现

https://github.com/PyGithub/PyGithub

github/__init__.py

from github.MainClass import Github

在这里插入图片描述
github/MainClass.py

from Requester import Requester

在这里插入图片描述
github/Requester.py
在这里插入图片描述
使用将账号密码base64,然后加入到Header中进行Basic 认证。

API:
search_repositories():使用GET /search/repositories
search_users()
search_issues():使用GET /search/issues
search_code():使用GET /search/code
search_commits()

搜索代码,实际发出请求的是:

github/MainClass.py中的search_code()

最后的return语句:
在这里插入图片描述
github/PaginatedList.py中,只有手动调用get_page()时才发出了HTTP请求。
在这里插入图片描述
看看requestJsonAndCheck()具体怎么实现的:
在这里插入图片描述
可见,其实其Requester也是通过requests这个库来实现的。
在这里插入图片描述
最终通过__requestRaw() 实现。
在这里插入图片描述
ContentFile 有这些属性:

content、decoded_content、download_url、encoding、git_url、html_url、license、name、path、repository、sha、size、type、url

参考:

https://github.com/PyGithub/PyGithub/blob/78d283b9b5c5222dd0ca2750db7b56a2a5545a39/github/ContentFile.py

再看一下token是如何传递的:
github/Requester.py中,
token是拼接之后放到了这个请求头中。
在这里插入图片描述

最终调用HTTP请求的是这个方法:
__requestEncode

    def __requestEncode(self, cnx, verb, url, parameters, requestHeaders, input, encode):
        # 限定HTTP请求动词只有这几个
        assert verb in ["HEAD", "GET", "POST", "PATCH", "PUT", "DELETE"]
        if parameters is None:
            parameters = dict()
        if requestHeaders is None:
            requestHeaders = dict()
        
        #进行认证
        self.__authenticate(url, requestHeaders, parameters)
        requestHeaders["User-Agent"] = self.__userAgent
        if self.__apiPreview:
            requestHeaders["Accept"] = "application/vnd.github.moondragon+json"

        url = self.__makeAbsoluteUrl(url)
        url = self.__addParametersToUrl(url, parameters)

        encoded_input = None
        if input is not None:
            requestHeaders["Content-Type"], encoded_input = encode(input)

        self.NEW_DEBUG_FRAME(requestHeaders)

        # 发出请求
        status, responseHeaders, output = self.__requestRaw(cnx, verb, url, requestHeaders, encoded_input)

        if Consts.headerRateRemaining in responseHeaders and Consts.headerRateLimit in responseHeaders:
            self.rate_limiting = (int(responseHeaders[Consts.headerRateRemaining]), int(responseHeaders[Consts.headerRateLimit]))
        if Consts.headerRateReset in responseHeaders:
            self.rate_limiting_resettime = int(responseHeaders[Consts.headerRateReset])

        if Consts.headerOAuthScopes in responseHeaders:
            self.oauth_scopes = responseHeaders[Consts.headerOAuthScopes].split(", ")

        self.DEBUG_ON_RESPONSE(status, responseHeaders, output)

        return status, responseHeaders, output

在这里插入图片描述
__authenticate()方法中,会判断若__authorizationHeader不为None,则将请求头设置为 __authorizationHeader
在这里插入图片描述

首先,你需要确定你要获取哪个网站上的文件点赞数,因为每个网站的点赞数获取方式都有所不同。比如,Github的点赞数可以通过API获取,而其他网站可能需要模拟用户操作进行爬取。 接下来,我们假设你要获取Github上某个仓库的点赞数,并进行可视化展示。可以使用PyGithub库来连接Github API进行操作,使用matplotlib库进行可视化展示。 以下是一个示例代码,获取某个Github仓库的点赞数并进行可视化展示: ```python from github import Github import matplotlib.pyplot as plt # Github API认证 g = Github("<your_access_token>") # 选择某个仓库 repo = g.get_repo("owner/repo_name") # 获取仓库的所有stars stars = [s for s in repo.get_stargazers()] # 统计每个用户的star数 star_counts = {} for s in stars: if s.login in star_counts: star_counts[s.login] += 1 else: star_counts[s.login] = 1 # 排序并取出前10个用户 top_stars = sorted(star_counts.items(), key=lambda x: x[1], reverse=True)[:10] # 可视化展示 plt.bar([x[0] for x in top_stars], [x[1] for x in top_stars]) plt.title("Top 10 Users with Most Stars") plt.xlabel("User") plt.ylabel("Number of Stars") plt.show() ``` 需要注意的是,获取Github API需要认证,需要在Github上申请一个access token,替换代码中的`<your_access_token>`。 另外,需要安装PyGithub和matplotlib库。可以使用以下命令进行安装: ``` pip install PyGithub matplotlib ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值