在使用Python爬虫时,经常遇见具有反爬机制的网站。我们可以通过伪装headers来爬取,但是网站还是可以获取你的ip,从而禁掉你的ip来阻止爬取信息。
在request方法中,我们可以通过proxies参数来伪装我们的ip,一些网站上有免费的ip代理网站,可以通过爬取这些ip,经检测后建立ip代理池。

ip代理网站:
(https://www.xicidaili.com/nt/)
(https://www.kuaidaili.com/free/intr/)

推荐

  • 推荐一种常用的伪装头方法
from fake_useragent import UserAgent
ua = UserAgent()
headers = {'User-Agent':ua.random}
  • 推荐一个2020最牛Python学习圈子,大家可以点击进入看看

接下来进入正题

爬取ip(IPPool.py)

import requests
from lxml import etree
from fake_useragent import UserAgent
#伪装
ua = UserAgent()
headers = {'User-Agent':ua.random}
def get_ip():ip_list = []#路径url = 'https://www.xicidaili.com/nt/' #ip是有时效的,只爬取第一页#请求response = requests.get(url=url,headers=headers)#设置编码response.encoding = response.apparent_encodingresponse = response.textresponse = etree.HTML(response)tr_list = response.xpath('//tr[@class="odd"]')for i in tr_list:#ipip = i.xpath('./td[2]/text()')[0]#端口号port = i.xpath('./td[3]/text()')[0]#协议agreement = i.xpath('./td[6]/text()')[0]agreement = agreement.lower()#拼装完整路径ip = agreement + '://' + ip + ':' + portip_list.append(ip)return ip_list
if __name__ == '__main__':ip_list = get_ip()print(ip_list)

测试ip

测试方法一(from multiprocessing.dummy import Pool)

import requests
from multiprocessing.dummy import Pool
#获取爬取到的ip列表
from IPPool import get_ip
test_list = get_ip()
#定义一个全局列表,用来存放有效ip
ip_list = []
#ip测试网站
url = 'http://icanhazip.com'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0'
}
def ip_test(ip):try:if ip.split(":")[0] == 'http':proxies = {'http': ip}else:proxies = {'https': ip}response = requests.get(url=url, headers=headers, proxies=proxies, timeout=3)ip_list.append(ip)print(ip + "可用")except:print(ip + "不可用")
if __name__ == '__main__':pool = Pool(4)pool.map(ip_test, test_list)print(ip_list)print("总共爬取%s个ip,可用ip为:%s,不可用ip为:%s"%(len(test_list),len(ip_list),len(test_list)-len(ip_list)))

测试结果:

测试方法二(Threading多线程队列)

import threading
import requests
import queue
from fake_useragent import UserAgent#获取爬取到的ip列表
from IPPool import get_ip
test_list = get_ip()
#定义一个全局列表,用来存放有效ip
ip_pool = []
#随机头伪装
ua = UserAgent()
headers = {'User-Agent':ua.random}url = 'https://www.csdn.net/'
# url = 'http://icanhazip.com/'def test_ip(queue_list):while True:if queue_list.empty():breakelse:ip = queue_list.get()if ip.split(":")[0] == 'http':proxies = {'http' : ip}else:proxies = {'https': ip}try:response = requests.get(url=url, headers=headers, proxies=proxies,timeout=3)if response.status_code == 200:print("【%s】测试%s,测试结果【可用】" % (threading.current_thread().name, proxies))ip_pool.append(ip)except:print("【%s】测试%s,测试结果【不可用】" % (threading.current_thread().name, proxies))if __name__ == '__main__':queue_list = queue.Queue()#创建队列#将爬取的ip放入队列中for i in test_list:queue_list.put(i)#创建线程out_thread = [threading.Thread(target=test_ip, args=(queue_list,), name="进程%s" % item) for item in range(5)]for thread in out_thread:thread.start()for thread in out_thread:thread.join()print('测试完成')print(ip_pool)print("总共爬取%s个ip,可用ip为:%s,不可用ip为:%s"%(len(test_list),len(ip_pool),len(test_list)-len(ip_pool)))

结果:

测试网址不需要那么复杂,www.baidu.com一类的都可以,有一位博主推荐了一个测试网站:http://icanhazip.com/

在测试时遇到了一个坑,没有太注意协议是http还是https,统一用了http,然后发现每一个ip都可以用,当然这是不可能的,经过修改后,测试成功的ip大概在二十五个左右。

https://www.kuaidaili.com/free/intr/这个网址的ip爬取也写了(ip还没有处理),但是这个网址的一页ip有点少,所以就没有测试

IPPool2.py

import requests
from lxml import etree
from fake_useragent import UserAgent
#伪装
ua = UserAgent()
headers = {'User-Agent':ua.random}def get_ip():ip_list = []#路径url = 'https://www.kuaidaili.com/free/intr/'#请求response = requests.get(url=url,headers=headers)#设置编码response.encoding = response.apparent_encodingresponse = response.textresponse = etree.HTML(response)tr_list = response.xpath('//*[@id="list"]/table/tbody/tr')for i in tr_list:ip = i.xpath('./td[1]/text()')[0]ip_list.append(ip)return ip_list
if __name__ == '__main__':ip_list = get_ip()# print(ip_list)

Python爬虫——建立IP代理池相关推荐

  1. (廿九)Python爬虫:IP代理池的开发

    作为一个爬虫开发者,使用IP代理是必要的一步,我们可以在网上找到免费的高匿IP,比如西刺代理.但是,这些免费的代理大部分都是不好用的,经常会被封禁.所以我们转而考虑购买付费代理.可是,作为一个程序员首 ...

  2. Python创建免费Ip代理池,伪装Ip。

    Python创建免费Ip代理池 主要使用requests第三方库.欸嘿,有了这个,就不用花钱买Ip了,生活小妙招.妙哇. 一.具体思路 1.利用requests爬取免费代理Ip的网页 2.存储列表后, ...

  3. python爬虫ip代理池_爬虫教程-Python3网络爬虫开发——IP代理池的维护

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 准备工作 要实现IP代理池我们首先需要成功安装好了 Redis 数据库并启动服务,另外还需要安装 Aiohttp.Requests.RedisPy.PyQ ...

  4. Python搭建自己[IP代理池]

    IP代理是什么: ip就是访问网页数据服务器位置信息,每一个主机或者网络都有一个自己IP信息 为什么要使用代理ip: 因为在向互联网发送请求中,网页端会识别客户端是真实用户还是爬虫程序,在今天以互联网 ...

  5. python利用proxybroker构建爬虫免费IP代理池!不用担心被封了!

    大纲 前言 ProxyBroker简介 ProxyBroker安装 在终端使用ProxyBroker 在代码中使用ProxyBroker 总结 前言 写爬虫的小伙伴可能遇到过这种情况: 正当悠闲地喝着 ...

  6. python建立ip代理池_Python搭建代理IP池实现存储IP的方法

    上一文写了如何从代理服务网站提取 IP,本文就讲解如何存储 IP,毕竟代理池还是要有一定量的 IP 数量才行.存储的方式有很多,直接一点的可以放在一个文本文件中,但操作起来不太灵活,而我选择的是 My ...

  7. 【Python爬虫建立IP池错误】爬取西刺网出现的各种问题

    本想爬取一个网站,但由于访问次数多了,遭服务器拒绝.后面就行通过建立一个IP池,当然就想爬取西刺网上的IP.所以就在网上copy了一份代码,但很不幸的是不管怎么弄都无法运行.所以我开始简化代码,从爬取 ...

  8. Python建立ip代理池(多线程)

    转载自公众号:JAVAandPythonJun 说在前面的话 Hello,我是JAP君,相信经常使用爬虫的朋友对代理ip应该比较熟悉,代理ip就是可以模拟一个ip地址去访问某个网站.我们有时候需要爬取 ...

  9. 【爬虫】IP代理池调研

    调研产品 芝麻IP,太阳IP,多贝代理.阿布云.亿牛云.西刺代理ip 阿布云 ¥1/时¥16/天¥108/周¥429/月 每个请求一个随机IP 海量IP资源池需求 近300个区域全覆盖 IP切换迅速使 ...

最新文章

  1. 软件调试的艺术笔记:GDB
  2. codeforce843B Interactive LowerBound
  3. mysql解析运行时间_分析 MySQL 语句运行时间
  4. 按需生产 ,我们准备好了吗?
  5. 用php写shell,php与shell实现多线程的简单例子
  6. java 之 桥接模式(大话设计模式)
  7. 魔兽服务器联盟在线,《魔兽世界》怀旧服再开新服,部落联盟泾渭分明?
  8. 使用VMWareWorkstation搭建学习环境
  9. mysql 在结果集中随机_MySql从表结果集中随机取一条数据
  10. VUE3.x(v-for)循环遍历指令
  11. sublime自动保存(失去焦点自动保存)
  12. jQuery stop()浅析
  13. linux如何让普通用户有root权限
  14. python : yield 的工作机制
  15. mysql设置远程访问的权限
  16. IE恶意修改防护大法(转)
  17. Edge上的gmail网页改为纯文字模式后改不回标准模式(HTML Gmail)问题和解决方法
  18. RPO和RTO是什么?
  19. 微信ios浏览器 与 iframe的不兼容问题
  20. 5 个无聊 Python 程序,用 Python 整蛊你的朋友们吧

热门文章

  1. PPT个人学习笔记(四)——神奇移动的实现
  2. Zk中组建显示模型mold都有哪些
  3. 企业为何需要建立统一的复用型软件平台?
  4. MongoDB年终大会转移至线上进行 | 周五参会指南
  5. 新走遍美国(一)---故事梗概
  6. 解决ubuntu安装后进入系统时黑屏,左上角闪动光标(ubuntu20亲测有效,应该是解释最详细的一篇了)
  7. View Invariant Gait Recognition Using Only One Uniform Model论文翻译以及理解
  8. 软件评测师——准备阶段
  9. html网页设计期末大作业——生物科技化妆品网页(6页) HTML+CSS+JS网页设计期末课程大作业
  10. 太炫酷了|对全国大学数据进行可视化分析,看完后发现.....