前言
主要目標是爬取Github上指定用戶的粉絲數據以及對爬取到的數據進行一波簡單的可視化分析。 讓我們愉快地開始吧~
開發工具
Python版本:3.6.4
相關模塊:
bs4模塊;
requests模塊;
argparse模塊;
pyecharts模塊;
以及一些python自帶的模塊。
環境搭建
安裝Python並添加到環境變量,pip安裝需要的相關模塊即可。
數據爬取
感覺好久沒用beautifulsoup了,所以今天就用它來解析網頁從而獲得我們自己想要的數據唄。以我自己的賬戶爲例:
我們先抓取所有關注者的用戶名,它在類似如下圖所示的標籤中:
用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
接着,我們就可以根據這些用戶名進入到他們的主頁來抓取對應用戶的詳細數據了,每個主頁鏈接的構造方式爲:
https://github.com/ + 用戶名
例如: https://github.com/CharlesPikachu
我們想要抓取的數據包括:
同樣地,我們利用beautifulsoup來提取這些信息:
for idx, name in enumerate(follower_names):
print('[INFO]: 正在爬取用戶%s的詳細信息...' % 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 = ''
# --倉庫數, stars數, 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)
# --貢獻數(最近一年)
num_contributions = soup.find_all('h2', class_='f4 text-normal mb-2')
num_contributions = self.str2int(num_contributions[0].text.replace('\n', '').replace(' ', ''). \
replace('contributioninthelastyear', '').replace('contributionsinthelastyear', ''))
# --保存數據
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))
數據可視化
這裏以我們自己的粉絲數據爲例,大概1200條吧。
先來看看他們在過去一年裏提交的代碼次數分佈吧:
提交最多的一位名字叫fengjixuchui,在過去一年一共有9437次提交。平均下來,每天都得提交20多次,也太勤快了。
再來看看每個人擁有的倉庫數量分佈唄:
本以爲會是條單調的曲線,看來低估各位了。
接着來看看star別人的數量分佈唄:
還行,至少不全都是"潛水白嫖"的
。表揚一下名爲lifa123的老哥,竟然給別人了18700個👍,也太秀了。
再來看看這1000多個人擁有的粉絲數量分佈唄:
簡單看了下,有不少小夥伴的followers數量比我還多。果然高手在民間。
看完篇文章喜歡的朋友點個愛心支持一下,關注我每天分享Python數據爬蟲案例,下篇文章分享是Python爬取並簡單分析A股公司數據
All done~完整源代碼詳見個人簡介或者私信獲取相關文件。