O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Python Requests Essentials

Book Description

Learn how to integrate your applications seamlessly with web services using Python Requests

In Detail

Python is one of the most popular programming languages of our era; the Python Requests library is one of the world's best clients, with the highest number of downloads. It allows hassle-free interactions with web applications using simple procedures.

You will be shown how to mock HTTP Requests using HTTPretty, and will learn to interact with social media using Requests. This book will help you to grasp the art of web scraping with the BeautifulSoup and Python Requests libraries, and will then paddle you through Requests impressive ability to interact with APIs. It will empower you with the best practices for seamlessly drawing data from web apps. Last but not least, you will get the chance to polish your skills by implementing a RESTful Web API with Python and Flask!

What You Will Learn

  • Demonstrate the use of Python Requests with the help of examples
  • Use the Requests module to deal with the inner sections of the request-response cycles
  • Implement the RESTful Web API with Python Requests
  • Authenticate Requests using different authentication methods
  • Emulate server actions and interact with a mock server
  • Interact with social networking sites such as Facebook, Twitter, and reddit
  • Scrape the Web with Python Requests and BeautifulSoup
  • Build your own web application with Flask

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Python Requests Essentials
    1. Table of Contents
    2. Python Requests Essentials
    3. Credits
    4. About the Authors
    5. Acknowledgments
    6. About the Reviewers
    7. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    8. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    9. 1. Interacting with the Web Using Requests
      1. Introduction to HTTP request
      2. Python modules
      3. Requests versus urllib2
      4. Essence of Requests
      5. Making a simple request
      6. Response content
      7. Different types of request contents
        1. Custom headers
        2. Sending form-encoded data
        3. Posting multipart encoded files
      8. Looking up built-in response status codes
      9. Viewing response headers
      10. Accessing cookies with Requests
      11. Tracking redirection of the request using request history
      12. Using timeout to keep productive usage in check
      13. Errors and exceptions
      14. Summary
    10. 2. Digging Deep into Requests
      1. Persisting parameters across Requests using Session objects
      2. Revealing the structure of a request and response
      3. Using prepared Requests
      4. Verifying an SSL certificate with Requests
      5. Body Content Workflow
        1. The Keep-alive facility
        2. Streaming uploads
      6. Using generator for sending chunk encoded Requests
      7. Getting the request method arguments with event hooks
      8. Iterating over streaming APIs
        1. Encodings
        2. HTTP verbs
      9. Self-describing the APIs with link headers
      10. Transport Adapter
      11. Summary
    11. 3. Authenticating with Requests
      1. Basic authentication
        1. Using basic authentication with Requests
      2. Digest authentication
        1. Using Digest authentication with Requests
      3. Kerberos authentication
        1. Using Kerberos authentication with Requests
      4. OAuth authentication
        1. OAuth 1.0
        2. Using OAuth 1.0 authentication with Requests
        3. OAuth 2.0
      5. Custom authentication
      6. Summary
    12. 4. Mocking HTTP Requests Using HTTPretty
      1. Understanding HTTPretty
      2. Installing HTTPretty
      3. Working with HTTPretty
      4. Setting headers
      5. Working with responses
        1. Rotating responses
        2. Streaming responses
        3. Dynamic responses through callbacks
      6. Summary
    13. 5. Interacting with Social Media Using Requests
      1. API introduction
        1. Getting started with the Twitter API
        2. Obtaining an API Key
        3. Creating an authentication Request
        4. Getting your favorite tweet
        5. Performing a simple search
        6. Accessing the list of followers
        8. Accessing available trends
        9. Updating user status
      2. Interacting with Facebook
        1. Getting started with the Facebook API
        2. Obtaining a key
        3. Getting a user profile
        4. Retrieving a friends list
        5. Retrieving feed
        6. Retrieving albums
      3. Interacting with reddit
        1. Getting started with the reddit API
        2. Registering a new account
        3. Modifying account information
        4. Performing a simple search
        5. Searching subreddits
      4. Summary
    14. 6. Web Scraping with Python Requests and BeautifulSoup
      1. Types of data
        1. Structured data
        2. Unstructured data
        3. Semistructured data
      2. What is web scraping?
        1. Dos and don'ts of web scraping
        2. Predominant steps to perform web scraping
      3. Key web scraping tasks
      4. What is BeautifulSoup?
        1. Document parsers
        2. Installation
        3. Objects in BeautifulSoup
          1. Tags
          2. BeautifulSoup
          3. NavigableString
        4. Web scraping tasks related to BeautifulSoup
        5. Searching the tree
          1. Navigating within the tree
            1. Navigating down
            2. Navigating sideways
            3. Navigating up
            4. Navigating back and forth
        6. Modifying the Tree
      5. Building a web scraping bot – a practical example
        1. The web scraping bot
          1. Identifying the URL or URLs
          2. Using an HTTP client
          3. Discovering the pieces of data to scrape
          4. Utilizing a web scraping tool
          5. Drawing the desired data
      6. Summary
    15. 7. Implementing a Web Application with Python Using Flask
      1. What is Flask?
      2. Getting started with Flask
      3. Installing Flask
        1. Installing required packages with pip
      4. Survey – a simple voting application using Flask
        1. Basic file structures
        2. Building the application
        3. Writing models with Flask-SQLAlchemy
        4. Defining a model
        5. Creating a database instance
          1. Creating survey models
          2. Creating tables in the database
          3. Querying database models
        1. List of all questions
        2. New survey
        3. Creating a new survey
        4. Displaying a survey
        5. Updating a survey
        6. Deleting a survey
        7. New vote form to caste a vote in a survey
        8. Casting a vote to a particular choice in a survey
      6. Templates
        1. The base template
        2. The list of questions template
        3. Creating a new survey template
        4. Showing the details of a survey template
        5. Casting a vote template
      7. Running the survey application
      8. Writing unit tests to survey applications
      9. Summary
    16. Index