0

i am trying build a github info in python, i want the script grab this things:

 "stargazers_count": 2, << i need the number updated
 "watchers_count": 2, << i need the number updated
 "forks": 1, << i need the number updated

ex: https://api.github.com/repos/toddmotto/angular-1-5-components-app

  import requests

  r = requests.get('https://api.github.com/repos/toddmotto/angular-1-5-components-app')

  print(r.json())

i need the results be like

stargazers_count: number

watchers_count: number

forks: number

1
  • Did you try something before asking? Commented Mar 5, 2019 at 11:17

6 Answers 6

1

Extract the required value using the key.

Ex:

import requests

r = requests.get('https://api.github.com/repos/toddmotto/angular-1-5-components-app').json()
print(r["stargazers_count"])
print(r["watchers_count"])
print(r["forks"])

Output:

573
573
200
Sign up to request clarification or add additional context in comments.

Comments

0

r.json() provides you a python dict.

  import requests
  r = requests.get('https://api.github.com/repos/toddmotto/angular-1-5-components-app')
  resp = r.json()
  print(r['stargazers_count']) #573
  print(r['watchers_count'])  #573
  print(r['forks']) #200

Comments

0

You can just put the json keys in a list and then iterate over the list picking out the values you need.

from requests import get

keys = [
    "stargazers_count",
     "watchers_count",
     "forks"
]

response = get('https://api.github.com/repos/toddmotto/angular-1-5-components-app').json()

for key in keys:
    print('{key}:\t{value}'.format(
            key=key,
            value=response[key]
        )
    )

use response.keys() if you need all keys

# prints out all keys
for key in response.keys():
print('{key}:\t{value}'.format(
        key=key,
        value=response[key]
    )
)

Comments

0

Using the Requests Library in Python

First things first, let’s introduce you to Requests.

What is the Requests Resource?

Requests is an Apache2 Licensed HTTP library, written in Python. It is designed to be used by humans to interact with the language. This means you don’t have to manually add query strings to URLs, or form-encode your POST data. Don’t worry if that made no sense to you. It will in due time.

What can Requests do?

Requests will allow you to send HTTP/1.1 requests using Python. With it, you can add content like headers, form data, multipart files, and parameters via simple Python libraries. It also allows you to access the response data of Python in the same way.

In programming, a library is a collection or pre-configured selection of routines, functions, and operations that a program can use. These elements are often referred to as modules and stored in object format.

Libraries are important because you load a module and take advantage of everything it offers without explicitly linking to every program that relies on them. They are truly standalone, so you can build your own programs with them and yet they remain separate from other programs.

Think of modules as a sort of code template.

To reiterate, Requests is a Python library.

Importing the Requests Module

To work with the Requests library in Python, you must import the appropriate module. You can do this simply by adding the following code at the beginning of your script:

import requests

Of course, to do any of this – installing the library included – you need to download the necessary package first and have it accessible to the interpreter.

Making a Request

When you ping a website or portal for information this is called making a request. That is exactly what the Requests library has been designed to do.

To get a webpage you would do something like the following:

r = requests.get('https://api.github.com/repos/toddmotto/angular-1-5-components-app')
finalResult = r.json()
print "stargazers_count",finalresult["stargazers_count"]

Comments

0

Creating a function that will take care for different combinations of requested attributes.

Taking care of failures as well.

import requests
import pprint


def get_github_activity_attributes(url, attributes=[]):
    """ Do http call and return the full response as dict or a subset of the response """
    r = requests.get(url)
    if r.status_code == 200:
        if not attributes:
            # The caller asked to return all attributes
            return True, r.json()
        else:
            # The caller asked for subset of the attributes (assuming first level attributes only)
            data = r.json()
            return True, {attr: data[attr] for attr in attributes}
    else:
        return False, 'Request failed with status code {}.'.format(r.status_code)


ok, all_attributes = get_github_activity_attributes('https://api.github.com/repos/toddmotto/angular-1-5-components-app')
if ok:
    print('All attributes below')
    pprint.pprint(all_attributes)


ok, few_attributes = get_github_activity_attributes('https://api.github.com/repos/toddmotto/angular-1-5-components-app',
                                                    ['forks', 'watchers_count'])
if ok:
    print('Few attributes below')
    pprint.pprint(few_attributes)

Comments

0

need the results be like

stargazers_count: number

watchers_count: number

forks: number

The simplest technique is to use PyGithub

from getpass import getpass

from github import Github

gh = Github(
    login_or_token="mpenning",
    password=getpass("Github password for mpenning: ")
    )
repo = gh.get_repo("mpenning/ciscoconfparse")
print("stargazers_count:", repo.stargazers_count)
# There is a bug in `watchers_count`.  It returns number of stargazers.
print("watchers_count:", repo.watchers_count)
print("forks:", repo.forks_count)

stdout:

% python simple_pygithub_demo.py
Github password for mpenning:
stargazers_count: 647
watchers_count: 647
forks 195

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.