Python中如何使用Github用戶數(shù)據(jù)爬蟲

Python中如何使用Github用戶數(shù)據(jù)爬蟲,針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

成都創(chuàng)新互聯(lián)成立于2013年,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目網(wǎng)站制作、做網(wǎng)站網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元孝義做網(wǎng)站,已為上家服務(wù),為孝義各地企業(yè)和個人服務(wù),聯(lián)系電話:028-86922220

前言

主要目標(biāo)是爬取Github上指定用戶的粉絲數(shù)據(jù)以及對爬取到的數(shù)據(jù)進(jìn)行一波簡單的可視化分析。 讓我們愉快地開始吧~

開發(fā)工具

Python版本:3.6.4

相關(guān)模塊:

bs4模塊;

requests模塊;

argparse模塊;

pyecharts模塊;

以及一些python自帶的模塊。

環(huán)境搭建

安裝Python并添加到環(huán)境變量,pip安裝需要的相關(guān)模塊即可。

數(shù)據(jù)爬取

感覺好久沒用beautifulsoup了,所以今天就用它來解析網(wǎng)頁從而獲得我們自己想要的數(shù)據(jù)唄。以我自己的賬戶為例:

Python中如何使用Github用戶數(shù)據(jù)爬蟲

我們先抓取所有關(guān)注者的用戶名,它在類似如下圖所示的標(biāo)簽中:

Python中如何使用Github用戶數(shù)據(jù)爬蟲

用beautifulsoup可以很方便地提取它們:

'''獲得followers的用戶名'''
def getfollowernames(self):
    print('[INFO]: 正在獲取%s的所有followers用戶名...' % self.target_username)
    page = 0
    follower_names = []
    headers = self.headers.copy()
    while True:
        page += 1
        followers_url = f'https://github.com/{self.target_username}?page={page}&tab=followers'
        try:
            response = requests.get(followers_url, headers=headers, timeout=15)
            html = response.text
            if 've reached the end' in html:
                break
            soup = BeautifulSoup(html, 'lxml')
            for name in soup.find_all('span', class_='link-gray pl-1'):
                print(name)
                follower_names.append(name.text)
            for name in soup.find_all('span', class_='link-gray'):
                print(name)
                if name.text not in follower_names:
                    follower_names.append(name.text)
        except:
            pass
        time.sleep(random.random() + random.randrange(0, 2))
        headers.update({'Referer': followers_url})
    print('[INFO]: 成功獲取%s的%s個followers用戶名...' % (self.target_username, len(follower_names)))
    return follower_names

接著,我們就可以根據(jù)這些用戶名進(jìn)入到他們的主頁來抓取對應(yīng)用戶的詳細(xì)數(shù)據(jù)了,每個主頁鏈接的構(gòu)造方式為:

https://github.com/ + 用戶名
例如: https://github.com/CharlesPikachu

我們想要抓取的數(shù)據(jù)包括:

Python中如何使用Github用戶數(shù)據(jù)爬蟲

同樣地,我們利用beautifulsoup來提取這些信息:

for idx, name in enumerate(follower_names):
    print('[INFO]: 正在爬取用戶%s的詳細(xì)信息...' % name)
    user_url = f'https://github.com/{name}'
    try:
        response = requests.get(user_url, headers=self.headers, timeout=15)
        html = response.text
        soup = BeautifulSoup(html, 'lxml')
        # --獲取用戶名
        username = soup.find_all('span', class_='p-name vcard-fullname d-block overflow-hidden')
        if username:
            username = [name, username[0].text]
        else:
            username = [name, '']
        # --所在地
        position = soup.find_all('span', class_='p-label')
        if position:
            position = position[0].text
        else:
            position = ''
        # --倉庫數(shù), stars數(shù), followers, following
        overview = soup.find_all('span', class_='Counter')
        num_repos = self.str2int(overview[0].text)
        num_stars = self.str2int(overview[2].text)
        num_followers = self.str2int(overview[3].text)
        num_followings = self.str2int(overview[4].text)
        # --貢獻(xiàn)數(shù)(最近一年)
        num_contributions = soup.find_all('h3', class_='f4 text-normal mb-2')
        num_contributions = self.str2int(num_contributions[0].text.replace('\n', '').replace(' ', ''). \
                            replace('contributioninthelastyear', '').replace('contributionsinthelastyear', ''))
        # --保存數(shù)據(jù)
        info = [username, position, num_repos, num_stars, num_followers, num_followings, num_contributions]
        print(info)
        follower_infos[str(idx)] = info
    except:
        pass
    time.sleep(random.random() + random.randrange(0, 2))

數(shù)據(jù)可視化

這里以我們自己的粉絲數(shù)據(jù)為例,大概1200條吧。

先來看看他們在過去一年里提交的代碼次數(shù)分布吧:

Python中如何使用Github用戶數(shù)據(jù)爬蟲

提交最多的一位名字叫fengjixuchui,在過去一年一共有9437次提交。平均下來,每天都得提交20多次,也太勤快了。Python中如何使用Github用戶數(shù)據(jù)爬蟲

再來看看每個人擁有的倉庫數(shù)量分布唄:

Python中如何使用Github用戶數(shù)據(jù)爬蟲

本以為會是條單調(diào)的曲線,看來低估各位了。

接著來看看star別人的數(shù)量分布唄:

Python中如何使用Github用戶數(shù)據(jù)爬蟲

還行,至少不全都是"潛水白嫖"的Python中如何使用Github用戶數(shù)據(jù)爬蟲

。表揚一下名為lifa123的老哥,竟然給別人了18700個????,也太秀了。Python中如何使用Github用戶數(shù)據(jù)爬蟲

再來看看這1000多個人擁有的粉絲數(shù)量分布唄:

Python中如何使用Github用戶數(shù)據(jù)爬蟲

關(guān)于Python中如何使用Github用戶數(shù)據(jù)爬蟲問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。

網(wǎng)站標(biāo)題:Python中如何使用Github用戶數(shù)據(jù)爬蟲
文章出自:http://muchs.cn/article38/gdespp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google外貿(mào)網(wǎng)站建設(shè)、App設(shè)計品牌網(wǎng)站設(shè)計、定制開發(fā)微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化