Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Flask Templates
  • Jinja2
  • Flask-REST API
  • Python SQLAlchemy
  • Flask Bcrypt
  • Flask Cookies
  • Json
  • Postman
  • Django
  • Flask Projects
  • Flask Interview Questions
  • MongoDB
  • Python MongoDB
  • Python Database
  • ReactJs
  • Vue.Js
Open In App
Next Article:
Retrieve Image and File stored as a BLOB from MySQL Table using Python
Next article icon

Create GitHub API to fetch user profile image and number of repositories using Python and Flask

Last Updated : 29 Nov, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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.
    • Flask would be used.

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"
 
github_html = requests.get(f'https://github.com/{username}').text
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"
 
github_html = requests.get(f'https://github.com/{username}').text
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

# We import the Flask Class, an instance of
# this class will be our WSGI application.
from flask import Flask
 
# We create an instance of this class. The first
# argument is the name of the application’s module
# or package.
# __name__ is a convenient shortcut for
# this that is appropriate for most cases.This is
# needed so that Flask knows where to look for resources
# such as templates and static files.
app = Flask(__name__)
 
# We use the route() decorator to tell Flask what URL
# should trigger our function.
@app.route('/')
def github():
    return "Welcome to GitHubGFG!"
 
# main driver function
if __name__ == "__main__":
   
    # run() method of Flask class runs the
    # application on the local development server.
    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):
    github_html = requests.get(f'https://github.com/{username}').text
    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
     
    # Creating a dictionary for our data
    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:
        github_html = requests.get(f'https://github.com/{username}').text
        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
        # Creating a dictionary for our data
        result = {
            'imgUrl' : img_url,
            'numRepos' : repos,
        }
    except:
        result = {
            "message": "Invalid Username!"
        }, 400
    return result
 
if __name__ == "__main__":
    app.run(debug=True)
                      
                       


Next Article
Retrieve Image and File stored as a BLOB from MySQL Table using Python

K

kothawleprem
Improve
Article Tags :
  • Python
  • Python Flask
  • Python-projects
Practice Tags :
  • python

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences