【Python】爬虫使用代理IP

1、代理池

IP 代理池可以理解为一个池子,里面装了很多代理IP。

  • 池子里的IP是有生命周期的,它们将被定期验证,其中失效的将被从池子里面剔除
  • 池子里的ip是有补充渠道的,会有新的代理ip不断被加入池子中
  • 池子中的代理ip是可以被随机取出的

2、为什么要用代理池

一些大型的网站(尤其是电商类网站),为了禁止爬虫获取数据,会采取限制同一个IP地址的网络请求数量、请求频率等方式,进行网站反扒。

而使用IP代理池后的爬虫,就可以隐藏自己IP,并且随机更换请求时的IP地址,绕过该反爬机制,快速获取大量数据。

3、代理IP获取

一些网站提供免费的代理IP,这个不稳定,并且很多都不能用。

收费的网站(如需求量不大,白嫖注册赠送的 IP 即可):

4、代理IP使用

获取代理IP(以巨量IP为例):

代码:

python 复制代码
    api_url = 'http://v2.api.juliangip.com/dynamic/getips...'
    username = '13255667788'
    password = '123456'
    proxy_ip_list = requests.get(api_url).json()['data']['proxy_list']
    for proxy_ip in proxy_ip_list:
        proxies = {
            "http": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": proxy_ip},
            "https": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": proxy_ip},
        }
        target_url = 'https://myip.ipip.net/'
        resp = requests.get(target_url, proxies=proxies)
        print(f'{proxy_ip} ------ {resp.text}')

10、资料

相关推荐
两万五千个小时1 小时前
落地实现 Anthropic Multi-Agent Research System
人工智能·python·架构
哈里谢顿3 小时前
Python 高并发服务限流终极方案:从原理到生产落地(2026 实战指南)
python
用户83562907805118 小时前
无需 Office:Python 批量转换 PPT 为图片
后端·python
markfeng819 小时前
Python+Django+H5+MySQL项目搭建
python·django
GinoWi20 小时前
Chapter 2 - Python中的变量和简单的数据类型
python
JordanHaidee20 小时前
Python 中 `if x:` 到底在判断什么?
后端·python
ServBay20 小时前
10分钟彻底终结冗长代码,Python f-string 让你重获编程自由
后端·python
闲云一鹤21 小时前
Python 入门(二)- 使用 FastAPI 快速生成后端 API 接口
python·fastapi
Rockbean1 天前
用40行代码搭建自己的无服务器OCR
服务器·python·deepseek
曲幽1 天前
FastAPI + Ollama 实战:搭一个能查天气的AI助手
python·ai·lora·torch·fastapi·web·model·ollama·weatherapi