Python中如何实现换IP功能?
self.auth = HTTPProxyAuth(“te815w”, “te815w”)
self.proxies = {“http”: “112.29.173.117:888”}
self.auth_1 = HTTPProxyAuth(“te8152”, “te815w”)
self.proxies_1 = {“http”: “112.29.170.61:888”}
self.auth_list = [(self.auth, self.proxies), (self.auth_1, self.proxies_1)]
我是这样写的 报错,总是说我 ip 没有授权,我一个测试是没有问题的,搞成一个列表报错
enter into list
<requests.auth.HTTPProxyAuth object at 0x7fa4d5e236d8> {‘http’: ‘112.29.170.61:888’}
获取内容
<requests.auth.HTTPProxyAuth object at 0x7fa4d5e236d8>
{‘http’: ‘112.29.170.61:888’}
ticket=bbf405041bac5120a5547cf069cda1aa
id =21d2ec857faef2cf6df662abe4e41322
841442 http://vip.qimingpian.com/#/detailorg?src=magic&ticket=bbf405041bac5120a5547cf069cda1aa&id=21d2ec857faef2cf6df662abe4e41322
<html>
<head><meta http-equiv=“Content-Type” content=“text/html; charset=utf-8” /></head>
<body>
<h1>Unauthorized …</h1>
<h2>
IP Address: 60.205.219.253:35544<br>
MAC Address: <br>
Server Time: 2018-08-15 19:32:07<br>
Auth Result:
</h2>
</body>
</html>
Traceback (most recent call last):
File “run.py”, line 56, in <module>
run().get_spider()
File “run.py”, line 18, in get_spider
qimingpian().get_id_and_url()
File “/home/shenjianlin/my_project/qimingpian_person/qimingpian_person.py”, line 219, in get_id_and_url
self.get_agency_content(row_column_list[i - 1], row_column_list[i], auth, proxies,user_index)
File “/home/shenjianlin/my_project/qimingpian_person/qimingpian_person.py”, line 246, in get_agency_content
json_data = json.loads(response.text)
File “/usr/lib64/python3.4/json/init.py”, line 318, in loads
return _default_decoder.decode(s)
File “/usr/lib64/python3.4/json/decoder.py”, line 343, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File “/usr/lib64/python3.4/json/decoder.py”, line 361, in raw_decode
raise ValueError(errmsg(“Expecting value”, s, err.value)) from None
ValueError: Expecting value: line 1 column 1 (char 0)
Python中如何实现换IP功能?
在Python里换IP,通常指的是通过代理服务器来发送网络请求,让目标网站看到的是代理服务器的IP,而不是你本机的真实IP。这在实际开发中很常见,比如做数据采集时防止被封锁。
最直接的方法是使用 requests 库,它内置了对HTTP/HTTPS代理的支持。你只需要在发起请求时,通过 proxies 参数传入一个代理服务器的地址就行。
下面是一个最基础的代码示例:
import requests
# 定义你要使用的代理服务器地址
# 格式通常是:'http://<代理IP>:<端口>' 或 'https://<代理IP>:<端口>'
proxies = {
'http': 'http://123.45.67.89:8080',
'https': 'http://123.45.67.89:8080', # 注意,很多HTTP代理也用于HTTPS
}
url = 'http://httpbin.org/ip' # 这个网址会返回你当前的IP
try:
# 使用代理发送请求
response = requests.get(url, proxies=proxies, timeout=10)
response.raise_for_status() # 检查请求是否成功
print(f"通过代理访问,返回的IP是:{response.text}")
except requests.exceptions.RequestException as e:
print(f"请求出错:{e}")
代码解释:
proxies字典是关键,它告诉requests库对http和https协议分别使用哪个代理。- 代理地址需要你自己准备,可以是免费的(不稳定),也可以是付费的(更可靠)。上面代码里的
123.45.67.89:8080只是个占位符,你需要换成能用的。 - 我们访问
http://httpbin.org/ip来测试,它会以JSON格式返回它看到的客户端IP。如果成功,这里打印的应该是你代理服务器的IP,而不是你本机的。
一些重要的点:
- 代理类型:除了上面例子中的HTTP代理,还有SOCKS代理(支持更底层的协议)。使用SOCKS代理需要安装
requests[socks]包,然后在proxies字典里用'http': 'socks5://...'这样的格式。 - 认证:如果代理服务器需要用户名和密码,地址格式像这样:
'http://user:pass@ip:port'。 - 轮换IP:如果你想在程序里自动切换多个IP(比如有一批代理),最简单的做法就是准备一个代理IP的列表,每次请求时随机选一个用。
import random
proxy_list = [
'http://ip1:port',
'http://ip2:port',
# ... 更多代理
]
proxy = random.choice(proxy_list)
proxies = {'http': proxy, 'https': proxy}
response = requests.get(url, proxies=proxies)
总结:核心就是准备好代理地址,然后在requests.get/post时传给proxies参数。

