🎯 A customizable, anti-detection cloud browser powered by self-developed Chromium designed for web crawlers and AI Agents.👉Try Now
Back to Blog

How To Generate Random IPs for Web Scraping

Michael Lee
Michael Lee

Expert Network Defense Engineer

23-Sep-2025

Introduction
Generating random IPs is essential for web scraping. Without IP rotation, your requests risk being blocked. This guide explores 10 practical methods to generate random IPs in Python. For an easier and more reliable approach, consider using Scrapeless, which automates IP rotation.


1. Using a Static List of IPs

Conclusion: A predefined list allows simple random selection.

Steps:

  1. Create a list of IPs (ips = ["192.168.1.1", "192.168.1.2"]).
  2. Use random.choice() for selection.
python Copy
import random

ips = ["192.168.1.1", "192.168.1.2", "192.168.1.3"]
random_ip = random.choice(ips)
print(random_ip)

Application: Small scraping tasks with a controlled set of IPs.


2. Using Random IP Generation

Conclusion: Generate IPs programmatically for higher variability.

Steps:

python Copy
import random

def random_ip():
    return ".".join(str(random.randint(1, 254)) for _ in range(4))

print(random_ip())

Application: For testing or temporary scraping tasks where IP reputation is not critical.


3. Rotating Free Public Proxies

Conclusion: Free proxies provide immediate random IPs.

Steps:

  1. Fetch a proxy list from public sources.
  2. Randomly select a proxy for requests.
python Copy
import requests, random

proxies = ["http://111.111.111.111:8080", "http://222.222.222.222:8080"]
proxy = random.choice(proxies)
response = requests.get("https://example.com", proxies={"http": proxy, "https": proxy})

Application: Small-scale scraping with low budget.


4. Using Proxy Rotation Services

Conclusion: Paid providers deliver reliable random IPs.

Steps:

  1. Sign up for services like Luminati, ScraperAPI, or Scrapeless.
  2. Use their endpoints with your API key.
python Copy
import requests

api_url = "https://proxyprovider.com?api_key=YOUR_KEY&url=https://example.com"
response = requests.get(api_url)
print(response.text)

Application: Large-scale web scraping without manual IP management.


5. Generating Random IPs with Tor

Conclusion: Tor provides anonymous random IPs.

Steps:

  1. Install Tor and stem library.
  2. Connect via SOCKS proxy (127.0.0.1:9050).
python Copy
proxies = {"http": "socks5h://127.0.0.1:9050", "https": "socks5h://127.0.0.1:9050"}
response = requests.get("https://example.com", proxies=proxies)

Application: Anonymous scraping and bypassing regional restrictions.


6. Random IPs in Selenium

Conclusion: Selenium supports rotating IPs via browser proxy.

Steps:

python Copy
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import random

ips = ["111.111.111.111:8080", "222.222.222.222:8080"]
chrome_options = Options()
chrome_options.add_argument(f'--proxy-server={random.choice(ips)}')
driver = webdriver.Chrome(options=chrome_options)
driver.get("https://example.com")

Application: Automation tasks with multiple browser sessions.


7. Async IP Rotation with HTTPX

Conclusion: Async requests support high concurrency with random IPs.

python Copy
import httpx, asyncio, random

ips = ["111.111.111.111:8080", "222.222.222.222:8080"]

async def fetch(url):
    proxy = random.choice(ips)
    async with httpx.AsyncClient(proxies={"http": proxy, "https": proxy}) as client:
        r = await client.get(url)
        print(r.status_code)

asyncio.run(fetch("https://example.com"))

Application: High-speed web scraping.


8. Rotating IPs in Scrapy

Conclusion: Scrapy middleware can automatically assign random IPs.

Steps:

  1. Enable HttpProxyMiddleware.
  2. Define a list of proxies in settings.py.
python Copy
DOWNLOADER_MIDDLEWARES = {'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware': 1}
PROXY_LIST = ['http://111.111.111.111:8080', 'http://222.222.222.222:8080']

Application: Large spider projects needing many random IPs.


9. Using Scrapeless for IP Management

Conclusion: Scrapeless automates random IP assignment for all requests.

Benefits:

  • Eliminates manual IP management
  • Handles concurrency efficiently
  • Reduces block risks

Application: Suitable for professional scraping with minimal setup.


10. Dynamic IPs with Environment Variables

Conclusion: Environment variables allow configurable random IPs.

python Copy
import os, random
from dotenv import load_dotenv

load_dotenv()
ips = os.getenv("IPS").split(",")
random_ip = random.choice(ips)
print(random_ip)

Application: Flexible pipeline setup for various scraping tasks.


Comparison Summary

Method Ease of Use Cost Scalability Best Use Case
Static List Easy Free Low Small tasks
Random Generation Easy Free Medium Testing
Free Proxies Easy Free Medium Low-budget scraping
Paid Providers Easy Paid High Large projects
Tor Medium Free Low Anonymous scraping
Selenium Medium Free/Paid Medium Browser automation
HTTPX Async Medium Free High High concurrency
Scrapy Medium Free High Spider projects
Scrapeless Very Easy Paid High Professional scraping
Env Variables Medium Free Medium Configurable pipelines

Key Takeaways

  • Random IPs reduce blocking and improve scraping reliability.
  • Python supports multiple approaches, from manual to automated.
  • Scrapeless provides an easy, reliable, and scalable solution.

FAQ

Q1: Why generate random IPs?
To prevent blocks and ensure consistent scraping performance.

Q2: Can I use free IP lists?
Yes, but stability is limited. Paid solutions are recommended for large projects.

Q3: Does Scrapeless require coding?
Minimal coding is needed; it integrates with Python scripts seamlessly.

Q4: Can I rotate IPs in Selenium?
Yes, assign a random proxy in browser options.

Q5: How often should IPs rotate?
Depends on request volume and target site restrictions.


For a hassle-free experience, try Scrapeless to automatically generate and rotate random IPs.


References

At Scrapeless, we only access publicly available data while strictly complying with applicable laws, regulations, and website privacy policies. The content in this blog is for demonstration purposes only and does not involve any illegal or infringing activities. We make no guarantees and disclaim all liability for the use of information from this blog or third-party links. Before engaging in any scraping activities, consult your legal advisor and review the target website's terms of service or obtain the necessary permissions.

Most Popular Articles

Catalogue