Menu
Home
Forums
New posts
Search forums
What's new
New posts
New media
New media comments
New profile posts
Latest activity
Media
New media
New comments
Search media
Members
Current visitors
New profile posts
Search profile posts
Account Upgrades
Advertise
Marketplace
Money
PerfectMoney
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Home
Forums
Programming & Web Design
Programming
General Programming Chat
Simple Python Scraper Template with Proxy Rotation & Anti-Ban Delays
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Genesis Schuppe" data-source="post: 28900" data-attributes="member: 11171"><p>Hello BHW!</p><p>I've been browsing the forum for a while and noticed many guys here struggle with basic scraping blocks and proxy management. I wanted to share a clean, lightweight Python boilerplate that I use for my automation tasks.</p><p>It handles random User-Agents, rotating proxies from a list, and includes random backoff delays to prevent immediate bans.</p><p></p><p>import requests</p><p>import random</p><p>import time</p><p>from bs4 import BeautifulSoup</p><p></p><p># List of proxies to rotate (format: 'ip<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite7" alt=":p" title="Stick out tongue :p" loading="lazy" data-shortname=":p" />ort' or 'user<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite7" alt=":p" title="Stick out tongue :p" loading="lazy" data-shortname=":p" />ass@ip<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite7" alt=":p" title="Stick out tongue :p" loading="lazy" data-shortname=":p" />ort')</p><p>PROXY_LIST = [</p><p> "http://your_proxy1<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite7" alt=":p" title="Stick out tongue :p" loading="lazy" data-shortname=":p" />ort",</p><p> "http://your_proxy2<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite7" alt=":p" title="Stick out tongue :p" loading="lazy" data-shortname=":p" />ort",</p><p> "http://your_proxy3<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite7" alt=":p" title="Stick out tongue :p" loading="lazy" data-shortname=":p" />ort"</p><p>]</p><p></p><p># List of common User-Agents to mimic different browsers</p><p>USER_AGENTS = [</p><p> "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",</p><p> "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2.1 Safari/605.1.15",</p><p> "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"</p><p>]</p><p></p><p>def get_secure_session():</p><p> session = requests.Session()</p><p> </p><p> # Set random User-Agent</p><p> session.headers.update({"User-Agent": random.choice(USER_AGENTS)})</p><p> </p><p> # Set random Proxy if list is not empty</p><p> if PROXY_LIST:</p><p> proxy = random.choice(PROXY_LIST)</p><p> session.proxies = {"http": proxy, "https": proxy}</p><p> </p><p> return session</p><p></p><p>def fetch_data(url):</p><p> session = get_secure_session()</p><p> try:</p><p> # Added timeout to prevent script from hanging</p><p> response = session.get(url, timeout=10)</p><p> if response.status_code == 200:</p><p> return response.text</p><p> else:</p><p> print(f"[-] Failed with status code: {response.status_code}")</p><p> return None</p><p> except requests.exceptions.RequestException as e:</p><p> print(f"[-] Connection error: {e}")</p><p> return None</p><p></p><p># Example usage</p><p>if __name__ == "__main__":</p><p> target_url = "<a href="https://httpbin.org/ip" target="_blank">https://httpbin.org/ip</a>" # Use this to test proxy rotation</p><p> </p><p> print("[+] Starting request...")</p><p> html_content = fetch_data(target_url)</p><p> </p><p> if html_content:</p><p> print("[+] Success! Parsing data...")</p><p> # You can add your BeautifulSoup logic here</p><p> </p><p> # Random delay between 2 and 5 seconds to stay under the radar</p><p> time.sleep(random.uniform(2.5, 5.5))</p><p></p><p>Features included:</p><p>Simple HTTP session management using requests.</p><p>Randomized headers to avoid fingerprinting.</p><p>Basic try/except block to catch bad or dead proxies without crashing the script.</p><p>Feel free to copy, modify, and use it for your web scraping or data extraction tasks. Let me know if you need help adjusting this for dynamic JavaScript websites (Playwright/Selenium) — I might drop a guide on that later if you guys are interested!</p></blockquote><p></p>
[QUOTE="Genesis Schuppe, post: 28900, member: 11171"] Hello BHW! I've been browsing the forum for a while and noticed many guys here struggle with basic scraping blocks and proxy management. I wanted to share a clean, lightweight Python boilerplate that I use for my automation tasks. It handles random User-Agents, rotating proxies from a list, and includes random backoff delays to prevent immediate bans. import requests import random import time from bs4 import BeautifulSoup # List of proxies to rotate (format: 'ip:port' or 'user:pass@ip:port') PROXY_LIST = [ "http://your_proxy1:port", "http://your_proxy2:port", "http://your_proxy3:port" ] # List of common User-Agents to mimic different browsers USER_AGENTS = [ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2.1 Safari/605.1.15", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36" ] def get_secure_session(): session = requests.Session() # Set random User-Agent session.headers.update({"User-Agent": random.choice(USER_AGENTS)}) # Set random Proxy if list is not empty if PROXY_LIST: proxy = random.choice(PROXY_LIST) session.proxies = {"http": proxy, "https": proxy} return session def fetch_data(url): session = get_secure_session() try: # Added timeout to prevent script from hanging response = session.get(url, timeout=10) if response.status_code == 200: return response.text else: print(f"[-] Failed with status code: {response.status_code}") return None except requests.exceptions.RequestException as e: print(f"[-] Connection error: {e}") return None # Example usage if __name__ == "__main__": target_url = "[URL]https://httpbin.org/ip[/URL]" # Use this to test proxy rotation print("[+] Starting request...") html_content = fetch_data(target_url) if html_content: print("[+] Success! Parsing data...") # You can add your BeautifulSoup logic here # Random delay between 2 and 5 seconds to stay under the radar time.sleep(random.uniform(2.5, 5.5)) Features included: Simple HTTP session management using requests. Randomized headers to avoid fingerprinting. Basic try/except block to catch bad or dead proxies without crashing the script. Feel free to copy, modify, and use it for your web scraping or data extraction tasks. Let me know if you need help adjusting this for dynamic JavaScript websites (Playwright/Selenium) — I might drop a guide on that later if you guys are interested! [/QUOTE]
Name
Verification
Post reply
Home
Forums
Programming & Web Design
Programming
General Programming Chat
Simple Python Scraper Template with Proxy Rotation & Anti-Ban Delays
Top