python建立ip代理池_Python爬虫——建立IP代理池

在使用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}

接下来进入正题

爬取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_encoding

response = response.text

response = etree.HTML(response)

tr_list = response.xpath('//tr[@class="odd"]')

for i in tr_list:

#ip

ip = 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 + ':' + port

ip_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)))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值