python使用心得之获得github代码库列表

yipeiwu_com6年前Python基础

1.背景

项目需求,要求获得github的repo的api,以便可以提取repo的数据进行分析。研究了一天,终于解决了这个问题,虽然效率还是比较低下。

因为github的那个显示repo的api,列出了每个repo的详细信息,而且是json格式的。现在貌似还没有找到可以分析多个json格式数据的方法,所以用的是比较蠢得splite加re的方法。如果大家有更好的方法,不发留言讨论!

2.代码

import re
import os

def GetUrl(num):
 str = os.popen("curl -G https://api.github.com/repositories?since=%d"%(num)).read()
 pattern = '"url"'
 pattern1='repos'
 urls=str.split(',\n')  
 for i in urls:
  if pattern in i and pattern1 in i:   
#  text1=i.splite(':')
  text=re.compile('"(.*?)"').findall(i)[1]
  print text
if __name__=='__main__':
 GetUrl(1000)

    其中num的值指的是页面的id,我们可以做一个循环,不断增大num的值,就可以无限提取repo。因为github的api对于流量是有限制的,所以这么做是一个可行的方法。

效果如下(提取下来的repo的api地址):

https://api.github.com/repos/wycats/merb-core

https://api.github.com/repos/rubinius/rubinius

https://api.github.com/repos/mojombo/god

https://api.github.com/repos/vanpelt/jsawesome

https://api.github.com/repos/wycats/jspec

https://api.github.com/repos/defunkt/exception_logger

https://api.github.com/repos/defunkt/ambition

https://api.github.com/repos/technoweenie/restful-authentication

https://api.github.com/repos/technoweenie/attachment_fu

https://api.github.com/repos/topfunky/bong

https://api.github.com/repos/Caged/microsis

https://api.github.com/repos/anotherjesse/s3

https://api.github.com/repos/anotherjesse/taboo

https://api.github.com/repos/anotherjesse/foxtracs

https://api.github.com/repos/anotherjesse/fotomatic

https://api.github.com/repos/mojombo/glowstick

https://api.github.com/repos/defunkt/starling

https://api.github.com/repos/wycats/merb-more

https://api.github.com/repos/macournoyer/thin

https://api.github.com/repos/jamesgolick/resource_controller

https://api.github.com/repos/jamesgolick/markaby

https://api.github.com/repos/jamesgolick/enum_field

https://api.github.com/repos/defunkt/subtlety

https://api.github.com/repos/defunkt/zippy

https://api.github.com/repos/defunkt/cache_fu

https://api.github.com/repos/KirinDave/phosphor

相关文章

python实现登录密码重置简易操作代码

需求: 1.用户输入密码正确登录 2.用户输入密码错误退出并调用函数继续输入 3.用户输入密码符合原先给定的一个值时,允许用户重置密码,并且可以用新密码登录 4.输入三次后禁止输入 虽然...

python3 图片 4通道转成3通道 1通道转成3通道 图片压缩实例

我就废话不多说了,直接上代码吧! from PIL import Image # 通道转换 def change_image_channels(image, image_path):...

python3实现字符串的全排列的方法(无重复字符)

最近在学一些基础的算法,发现我的数学功底太差劲了,特别是大学的这一部分,概率论、线性代数、高数等等,这些大学学的我是忘得一干二净(我当时学的时候也不见得真的懂),导致现在学习算法,非常的...

在Django的上下文中设置变量的方法

前一节的例子只是简单的返回一个值。 很多时候设置一个模板变量而非返回值也很有用。 那样,模板作者就只能使用你的模板标签所设置的变量。 要在上下文中设置变量,在 render() 函数的c...

python批量生成本地ip地址的方法

本文实例讲述了python批量生成本地ip地址的方法。分享给大家供大家参考。具体分析如下: 这段代码用于在本地计算机上生成本地ip地址绑定到网卡,生成的是一个bat的批处理文件,运行此批...