
Hands-on with Python to tune proxy IP response
A lot of old iron in the crawl data have encountered the embarrassment of the IP is blocked, this time the proxy IP is a life-saving straw. Today we take ipipgo home proxy service as an example, teach you how to use Python to proxy IP play skilfully.
The right way to get a proxy IP
Let's start with the whole dry run, here's how ipipgo's API works (be careful to replace your account key):
import requests
def grip_proxy().
api_url = "https://api.ipipgo.com/get?format=json"
params = {
"key": "Your account key",
"count": 5, how many IPs
"protocol": "socks5" Choose a protocol that works.
}
resp = requests.get(api_url, params=params)
return [ip['proxy address'] for ip in resp.json()['data']]
Here's the point:Remember to wrap this code in try-except, network jerks are common. When you get the IP list, you'd better test the speed first, don't rush to work directly.
Response processing triple axe
Proxy IPs easy to roll over with? These three tricks save your life:
| symptomatic | antidote |
|---|---|
| stall | Set timeout=10 seconds, change IP if timeout expires |
| Returns strange status codes | If you encounter 403/429, write down the IP and use it again after a break. |
| Incomplete return of data | Check if the Content-Length in the response header is correct or not! |
Real-world code with comments
See this full example with exception handling added:
from random import choice
import requests
Proxy Pool = WeedProxy() with the previously written function
def secure_request(url).
for _ in range(3): give 3 chances
current proxy = {'https': choice(proxy pool)}
try.
resp = requests.get(url,
proxies=current proxy, timeout=15)
timeout=15)
if resp.status_code == 200:: If resp.status_code == 200.
Here you can add content validation
return resp.text
except Exception as Mothmaids.
print(f"{current agent} is pumping, reason: {str(moth)[:30]}...")
Proxy pool.remove(current proxy['https']) remove bad IPs
if len(proxy pool) < 2: replenish ammo in time
proxy pool.extend(gripproxy())
return None
Common Rollover Scene QA
Q: Proxy IPs are not working when I use them?
A: Normal phenomenon! Dynamic IPs are supposed to change, so it is recommended that you randomly select IPs for each request, like shuffling cards to disrupt the order of use.
Q: Snail-like response time?
A: Try ipipgo'sTK LineThe company is optimized for domestic access. If the budget is enough, directly on the static residential IP, 35 dollars a can use the whole month.
Q: What if I have to deal with a large number of requests?
A: on ipipgo's enterprise version of the dynamic residential package, more than 9 yuan 1G traffic, support for concurrent extraction of IP. remember to add the request interval in the code, don't blow up their servers.
A Little Tree of Thumb for Choosing Packages
The three packages, ipipgo, are the most commonly used:
- Dynamic standard version: suitable for novice practitioners, more than 7 yuan 1G traffic
- Dynamic Enterprise Edition: essential for teamwork, supports high concurrency
- Static residential IPs: a must for long term assignments with long IP survival times
The last thing I'd like to say is that if you mix proxy IPs with local IPs, you can effectively reduce the probability of being blocked. If you use ipipgo, remember that their API supports hourly billing, don't buy monthly packages for short tasks, and save money to buy ribs.

