If you want to keep your browsing private or bypass website restrictions, you can use a proxy with Python. In this guide, I’ll show you step-by-step instructions on how to use proxy with Python Requests.

Here’s the brief for you: To use a proxy with Python requests, you simply need to tell Python where to send your requests through the proxy by adding the proxy details when making the request. 

You just include the proxy's address and port in your code, and Python will send your requests through that proxy instead of directly to the website. 

Basically, it’s easy to follow and will help you use proxies with Python's requests library.

Key Takeaways:

  • Proxies help hide your real IP and access restricted content.

  • Python Requests makes it easy to use proxies.

  • Use the proxies parameter to add proxy details.

  • Rotate proxies to avoid blocks while scraping.

  • Proxy requests work with different HTTP methods like GET and POST.

What’s a Proxy?

A proxy is like a middleman between you and the website. When you use a proxy, the website sees the proxy’s address instead of yours. This can help you stay anonymous or access blocked content.

So, What’s the Speciality of Python Requests?

HTTP requests

Simple syntax to send HTTP requests with just a few lines of code.

types of requests

Works with GET, POST, PUT, DELETE, and more types of requests.

features to manage request

 Built-in features to manage request failures or timeouts.

Converts response data like JSON

Converts response data (like JSON or HTML) into usable formats.

Quickly fetches data from APIs

Quickly fetches data from websites or APIs.

7 Steps on How to Use Proxy with Python Requests

Now, let me share the overall process step-by-step.

Step 1: Install Python and Requests

Before you start, you need to have Python installed on your computer. If you haven’t done so, go to python.org and download the latest version.

Once Python is installed, you need the requests library. It's a simple tool that allows you to send HTTP requests with Python.


To install it, open your terminal or command prompt and run:

Pip install requests

This will install the requests library, which you can use to interact with websites.

Step 2: Get Your Proxy

To use a proxy, you need to have one. There are many proxy providers, some free and some paid, like Bright Data, Oxylabs, Smartproxy, Webshare, etc.

You can find proxies that are specifically made for web scraping or for just browsing privately.

For this example, let’s assume you have a proxy with the following details:

  • IP Address: 192.168.1.1
  • Port: 8080
  • Username (for authentication): user123
  • Password: pass456

This proxy will act as the middleman between you and the websites you're accessing.

Step 3: Setting Up the Proxy in Your Code

Now that you have your proxy, let’s integrate it into Python using the requests library.

Here's the basic setup:

Setting Up the Proxy in Your Code
  • proxy: A dictionary where you provide the proxy address for both HTTP and HTTPS.
  • requests.get(): This function sends a GET request to the website through the proxy.

What happens here is:

  • Define the proxy in a dictionary.
  • Tell requests.get() to use the proxy by passing it through the proxies parameter.

The website you’re trying to reach will see the proxy’s IP address instead of yours. This provides you with anonymity or allows you to bypass restrictions.

Read this blog to get a detailed idea of how to set a proxy in Python code.

Step 4: Add Proxy Authentication (If Needed)

Some proxies need authentication to work. If your proxy requires a username and password, you can add these details in the URL format.

Here's how you’d do it:

Add Proxy Authentication
  • user123:pass456@: This is where you add the authentication details (username and password) before the proxy’s IP address.

Now, your code includes the username and password for the proxy.

Step 5: Handle Errors

Sometimes, proxies don’t work as expected—either due to incorrect details or network issues. It’s a good idea to handle errors in your code so you know when something goes wrong.

Here’s how you can handle errors:

incorrect details or network issues
  • raise_for_status(): This will raise an error if the request doesn’t succeed (for example, if the website is down or the proxy is wrong).
  • RequestException: Catches any kind of error during the request process, like a wrong proxy or timeout.

This will print an error message if something goes wrong.

Step 6: Rotating Proxies (If You Need Multiple)

If you’re working on a large project, such as scraping many pages, you might need to use different proxies to avoid getting blocked.

Here’s how you can rotate proxies.

Let’s say you have a list of proxies, and you want your script to try each one:

Rotating Proxies (If You Need Multiple)

This will attempt to use each proxy in the list until it finds one that works. 

It helps if you have many proxies and need to avoid hitting rate limits.

Step 7: Be Responsible

It’s important to remember that proxies can be used to bypass restrictions, but some websites may block or limit requests coming from proxies.

Always be respectful of the website's terms of service and avoid scraping too aggressively.

If you’re interested in learning how to set a proxy programmatically in an Android app, check out this informative blog now.

How to Rotate IPs with Python Requests

Rotating IPs helps you avoid getting blocked while scraping websites. Here's how you can do it step by step:

01. Get a List of Proxies:

  • You need a list of proxy IPs. These are servers that will help hide your real IP while sending requests. You can find free proxy lists online or use your own.

02. Set Up Your Script:

First, you’ll need to import some tools:

  • requests: To send requests to websites.
  • random: To pick random proxies.
  • BeautifulSoup (optional for scraping HTML content).

03. Create the Proxy Method:

  • Write a method (get_proxy()) that gets a random proxy from your list. You’ll use the random function to choose one proxy from the list.

04. Define a Proxy Request Method:

The proxy_request() method will use the proxy to send a request. 

It takes:

  • request_type: What kind of request you want (like GET or POST).
  • url: The website you want to scrape.
  • kwargs: Extra details like headers.

05. Try Different Proxies:

  • If one proxy doesn't work, the method will try the next one until it succeeds.

06. Make the Request:

  • Finally, use the proxy_request() method to send the request and rotate through different proxies.

Example in Simple Terms

1. You have a list of proxies:

ip_addresses = [ "http://proxy1.com", "http://proxy2.com", ... ]

2. The script picks a random proxy:

proxy = random.randint(0, len(ip_addresses) - 1)

3. It sends the request using that proxy:

response = requests.request(request_type, url, proxies=proxies, timeout=5)

If there’s an error (like the proxy is down), it will try another one until it works.

Different HTTP Methods in Python Requests

Here’s a table showing the different HTTP methods in Python Requests, along with their syntax and purpose:

Method

Syntax

Used to

GET

requests.get(url)

Retrieve data from a server

POST

requests.post(url, data=...)

Send data to a server to create or update a resource

PUT

requests.put(url, data=...)

Update an existing resource on the server

PATCH

requests.patch(url, data=...)

Partially update a resource on the server

DELETE

requests.delete(url)

Delete a resource on the server

HEAD

requests.head(url)

Retrieve the headers of a resource

OPTIONS

requests.options(url)

Retrieve the supported HTTP methods for a URL

Wrapping Up

Setting up a proxy with Python requests is easy and useful. It helps with things like web scraping, hiding your IP, or accessing content blocked in your area.

Follow these simple steps mentioned on how to use proxy with Python Requests to start using proxies in your projects. Install the tools, handle errors, and even rotate proxies when needed.

Now, you’re ready to use proxies in Python. Just remember to be responsible. Happy coding!

FAQs

What are the legal and ethical considerations of using proxies for web scraping?

Always follow the rules in robots.txt to avoid being blocked. Then, respect rate limits to prevent overloading servers. Basically, use proxies responsibly and never for harmful activities.

How can I handle proxy errors and timeouts effectively in Python Requests?

Use retry mechanisms like requests.adapters to automatically retry failed requests. Plus, set timeout values to avoid waiting too long for responses. Also, handle errors like connection issues with try-except blocks to prevent crashes.

How can I securely store proxy credentials in Python scripts?

Store proxy credentials in environment variables or configuration files to keep them safe. Even, you can use secure key management tools to prevent accidental exposure. Never hardcode credentials directly into your scripts.

Can I use proxies with asynchronous Python libraries like asyncio and aiohttp?

Yes, you can use proxies with asyncio and aiohttp. Configure proxies by adding them to the session’s parameters, similar to how it's done in requests. This helps handle many requests efficiently with proxies.

How can I monitor and optimize proxy performance in my Python scripts?

Track key metrics like response time and success rate to assess proxy performance. Analyze errors to pinpoint issues with specific proxies. Plus, continuously test and adjust proxy settings to improve performance.

About the Author

Jayden Sprent is a distinguished expert in Cybersecurity, Proxies, and Web Scraping. He graduated from California University with a strong foundation in Cybersecurity. With extensive experience and a deep understanding of digital security, Jayden is dedicated to protecting online environments and optimizing web data extraction. His expertise in proxies ensures efficient and secure internet connections, while his proficiency in web scraping enables valuable data acquisition. Jayden's commitment to excellence and personalized approach has made him a trusted professional in the field.

JUMP INTO THE CONVERSATION

Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}