Our first Launch Week is over! See the recap 🚀

Aug 9, 2024

•

Eric Ciarla imageEric Ciarla

How to easily install requests with pip and python

The requests library is the de facto standard for making HTTP requests in Python. It abstracts the complexities of making requests behind a beautiful, simple API so that you can focus on interacting with services and consuming data in your application.

Some use cases for requests include Web Scraping, API integration, data retrieval from web services, automated testing of web applications, and sending data to remote servers.

In this tutorial, we’ll cover several ways to install requests and demonstrate basic usage with some examples.

Installing requests

There are several ways to install requests depending on your needs and environment.

Using pip

The easiest way to install requests is using pip:

python -m pip install requests

Using conda

If you use the Anaconda distribution of Python, you can install requests using conda:

conda install requests

Using a virtual environment

It’s good practice to use a virtual environment to manage the dependencies for your Python projects. You can install requests in a virtual environment like so:

python -m venv myenv
source myenv/bin/activate  # On Windows, use `myenv\Scripts\activate`
pip install requests

From source

You can also install requests from source by downloading the code from the GitHub repo and running:

python setup.py install

Troubleshooting

If you encounter issues when trying to install requests, here are a few things to check:

  • Make sure you’re using a recent version of Python (3.6+).
  • If you’re using pip, make sure you’ve upgraded to the latest version with python -m pip install --upgrade pip.
  • If you’re using conda, make sure your Anaconda distribution is up-to-date.
  • Check that you have permissions to install packages on your system. You may need to use sudo or run your command shell as Administrator.

If you’re still having trouble, consult the requests documentation or ask for help on the requests GitHub issues page.

Usage Examples

With requests installed, let’s look at a couple of basic usage examples.

Making a Request

Let’s make a basic GET request to the GitHub API:

import requests

response = requests.get('https://api.github.com')

print(response.status_code)
# 200

print(response.text)
# '{"current_user_url":"https://api.github.com/user","authorizations_url":"https://api.github.com/authorizations", ...}'

We can see the response status code is 200, indicating the request was successful. The response text contains the JSON data returned by the API.

Web Scraping

requests is commonly used for web scraping. Let’s try scraping the HTML from a Wikipedia page:

import requests

url = 'https://en.wikipedia.org/wiki/Web_scraping'

response = requests.get(url)

print(response.status_code)
# 200

print(response.text)
# <!DOCTYPE html>
# <html class="client-nojs" lang="en" dir="ltr">
# <head>
# <meta charset="UTF-8"/>
# <title>Web scraping - Wikipedia</title>
# ...
# </body>
# </html>

We make a GET request to the Wikipedia article on web scraping and print out the complete HTML content of the page by accessing response.text. You can then use a library like BeautifulSoup to parse and extract information from this HTML.

For more advanced web scraping needs, consider using a dedicated scraping service like Firecrawl. Firecrawl handles the complexities of web scraping, including proxy rotation, JavaScript rendering, and avoiding detection, so you can focus on working with the data. Check out the Python SDK here.

References

Ready to Build?

Start scraping web data for your AI apps today.
No credit card needed.

About the Author

Eric Ciarla image
Eric Ciarla@ericciarla

Eric Ciarla is the Chief Operating Officer (COO) of Firecrawl and leads marketing. He also worked on Mendable.ai and sold it to companies like Snapchat, Coinbase, and MongoDB. Previously worked at Ford and Fracta as a Data Scientist. Eric also co-founded SideGuide, a tool for learning code within VS Code with 50,000 users.

More articles by Eric Ciarla