Skip to content

Instantly share code, notes, and snippets.

@saitjr
Last active May 21, 2017 02:57
Show Gist options
  • Save saitjr/e6b214d69cdd49a8f035c9b1bb2075d7 to your computer and use it in GitHub Desktop.
Save saitjr/e6b214d69cdd49a8f035c9b1bb2075d7 to your computer and use it in GitHub Desktop.
count total star of user repos. Install BeautifulSoup first.
#!/usr/bin/python
#-*- coding: utf-8 -*-
#encoding=utf-8
import urllib2
import re
from bs4 import BeautifulSoup
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
def delete_ln_space(text):
result = re.sub(r"[\n| ]", "", text)
return result
def star_in_url(url):
html = urllib2.urlopen(url).read()
soup = BeautifulSoup(html, "html.parser")
liResult = soup.findAll('div', id = "user-repositories-list")[0].findAll("li")
count = 0
for temp in liResult:
if len(temp.findAll("span", class_ = "text-gray")) != 0:
continue
a = temp.findAll("a", class_ = "muted-link mr-3")
if len(a) == 0:
continue
title = temp.findAll("h3")[0].text
star = a[0].text
count += int(star)
print delete_ln_space(title), " ", delete_ln_space(star)
return count
def repo_links(user_name):
html = urllib2.urlopen("https://github.com/" + user_name + "?tab=repositories").read()
soup = BeautifulSoup(html, "html.parser")
links = []
links.append("/" + user_name + "?page=1&tab=repositories")
# 获取分页列表
page_div = soup.findAll("div", class_ = "pagination")
# 无分页,直接返回第一页
if page_div is None or len(page_div) == 0:
return links
all_a = page_div[0].findAll("a")
# 丢弃 Next 页的 url
if all_a[-1:][0].text == "Next":
all_a = all_a[:-1]
for a in all_a:
links.append(a["href"])
return links
if __name__ == '__main__':
user_name = raw_input("user name >>> ")
all_links = repo_links(user_name)
count = 0
for link in all_links:
count += star_in_url("https://github.com/" + link)
print count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment