Create GitHub API to fetch user profile image and number of repositories using Python and Flask
Last Updated : 29 Nov, 2021
GitHub is where developers shape the future of software, together, contribute to the open-source community, manage Git repositories, etc. It is one of the most used tools by a developer and its profile is shared to showcase or let others contribute to its projects. Web Scraping using python is also one of the best methods to get data.
In this article, we will create an API to fetch a user’s profile image and its followers. Following is the flow in which this blog would guide to create an API:
- Setting up the App Directory
- Web Scrape data from GitHub.
- Beautiful Soup in Python would be used.
- Create an API.
Setting up the App Directory
Step 1: Create a folder (eg. GitHubGFG).
Step 2: Set up the virtual environment. Here we create an environment .env
python -m venv .env
Step 3: Activate the environment.
.env\Scripts\activate

Scraping the Data
Step 1: In Python, we have Beautiful Soup which is a library to pull out data from HTML files. To install Beautiful Soup, run a simple command;
pip install beautifulsoup4
Step 2: Install the Requests module of Python. Requests allows to send HTTP/1.1 requests extremely easily.
pip install requests
Create a python file. (eg: github.py)
Step 3: Following are the steps for Scraping data from the Web Page. To get the HTML text from the web page;
github_html = requests.get(f'https://github.com/{username}').text
The {username} will have the GitHub username of the required user. To represent the parsed object as a whole we use the BeautifulSoup object,
soup = BeautifulSoup(github_html, "html.parser")
Example:
Python3
from bs4 import BeautifulSoup
import requests
username = "kothawleprem"
soup = BeautifulSoup(github_html, "html.parser" )
print (soup)
|
Output:
Now find the avatar class in the HTML document as it has the required URL for the profile image.
find_all(): The find_all() method looks through a tag’s descendants and retrieves all descendants that match the filters. Here our filter is an img tag with the class as avatar.
Python3
avatar_block = soup.find_all( 'img' , class_ = 'avatar' )
print (avatar_block)
|
Following is the output of avatar_block:
The image URL is in the src attribute, to get the URL text use .get():
Python3
img_url = avatar_block[ 4 ].get( 'src' )
print (img_url)
|
Following is the output of img_url:
Find the first Counter class in the HTML document as it has the required data for the number of repositories.
find(): The find() method looks through a tag’s descendants and retrieves a single descendant that matches the filters. Here our filter is a span tag with the class as Counter.
repos = soup.find('span',class_="Counter").text
The entire code would be as follows:
Python3
from bs4 import BeautifulSoup
import requests
username = "kothawleprem"
soup = BeautifulSoup(github_html, "html.parser" )
avatar_block = soup.find_all( 'img' , class_ = 'avatar' )
img_url = avatar_block[ 4 ].get( 'src' )
repos = soup.find( 'span' , class_ = "Counter" ).text
print (img_url)
print (repos)
|
Output:
https://avatars.githubusercontent.com/u/59017652?v=4 33
Creating the API
We will use Flask which is a micro web framework written in Python.
pip install Flask
Following is the starter code for our flask application.
Python3
from flask import Flask
app = Flask(__name__)
@app .route( '/' )
def github():
return "Welcome to GitHubGFG!"
if __name__ = = "__main__" :
app.run(debug = True )
|
Open localhost on your browser:
Getting the GitHub username from the URL:
Python3
from flask import Flask
app = Flask(__name__)
@app .route( '/<username>' )
def github(username):
return f "Username: {username}"
if __name__ = = "__main__" :
app.run(debug = True )
|
Output:
We would now add our code of Web Scrapping and some helper methods provided by Flask to properly return JSON data. jsonify is a function in Flask. It serializes data to JavaScript Object Notation (JSON) format. Consider the following code:
Python3
import requests
from bs4 import BeautifulSoup
from flask import Flask
app = Flask(__name__)
@app .route( '/<username>' )
def github(username):
soup = BeautifulSoup(github_html, "html.parser" )
avatar_block = soup.find_all( 'img' , class_ = 'avatar' )
img_url = avatar_block[ 4 ].get( 'src' )
repos = soup.find( 'span' , class_ = "Counter" ).text
result = {
'imgUrl' : img_url,
'numRepos' : repos,
}
return result
if __name__ = = "__main__" :
app.run(debug = True )
|
Output:
If the username is not correct or for any other reason, we need to add our code in the try and except block to handle exceptions. The final code would be as follows:
Python3
import requests
from bs4 import BeautifulSoup
from flask import Flask
app = Flask(__name__)
@app .route( '/<username>' )
def github(username):
try :
soup = BeautifulSoup(github_html, "html.parser" )
avatar_block = soup.find_all( 'img' , class_ = 'avatar' )
img_url = avatar_block[ 4 ].get( 'src' )
repos = soup.find( 'span' , class_ = "Counter" ).text
result = {
'imgUrl' : img_url,
'numRepos' : repos,
}
except :
result = {
"message" : "Invalid Username!"
}, 400
return result
if __name__ = = "__main__" :
app.run(debug = True )
|
Similar Reads
Fetch top 10 starred repositories of user on GitHub | Python
Prerequisites: Basic understanding of python, urllib2 and BeautifulSoup We often write python scripts to make our task easier, so here is the script which helps you to fetch top 10 starred repositories of any user on GitHub.You just need Github username (For example: msdeep14) to run the script.Scri
4 min read
How to create GitHub repository using Python Selenium?
Prerequisite: Selenium Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc, we will be working with Python. Sel
3 min read
Retrieve Image and File stored as a BLOB from MySQL Table using Python
Prerequisites: MySQL server should be installed In this post, we will be talking about how we can store files like images, text files, and other file formats into a MySQL table from a python script. Sometimes, just like other information, we need to store images and files into our database and provi
3 min read
Python Tweepy â Getting the number of lists a user has been added to
In this article we will see how we can get the number of lists a user has been added to. The listed_count attribute provides us with an integer denoting the number of public lists a user has been added to. Private lists are not counted. In order to get the number number of public lists a user has be
2 min read
Profile Application using Python Flask and MySQL
A framework is a code library that makes a developer's life easier when building web applications by providing reusable code for common operations. There are a number of frameworks for Python, including Flask, Tornado, Pyramid, and Django. Flask is a lightweight web application framework. It is clas
10 min read
GET and POST Requests in GraphQL API using Python requests
In this article, we will be understanding how to write GET and POST requests to GRAPHQL APIs using the Python request module. Dealing with GraphQL API is a bit different compared to the simple REST APIs. We have to parse in a query that involves parsing the GraphQL queries in the request body. What
9 min read
Create API Tester using Python Requests Module
Prerequisites: Python Requests module, API In this article, we will discuss the work process of the Python Requests module by creating an API tester. API stands for Application Programming Interface (main participant of all the interactivity). It is like a messenger that takes our requests to a syst
3 min read
Python Tweepy â Getting the number of friends of a user
In this article we will see how we can get the number of friends of a user. A friend is an account which the user is following. The friends_count attribute provides us with and integer donating the number of friends the user account has. Identifying the number of friends in the GUI : In the above me
2 min read
Python Tweepy â Getting the number of followers of a user
In this article we will see how we can get the number of followers of a user. The followers_count attribute provides us with an integer donating the number of followers the user account has. Identifying the number of followers in the GUI : In the above mentioned profile the number of followers are :
2 min read
How to Create a basic API using Django Rest Framework ?
Django REST Framework is a wrapper over the default Django Framework, basically used to create APIs of various kinds. There are three stages before creating an API through the REST framework, Converting a Model's data to JSON/XML format (Serialization), Rendering this data to the view, and Creating
4 min read