爬虫篇-代理IP的获取/验证/使用【附源码】


前言

在日常爬虫中,很多网站都出现了反爬虫,一是模拟登陆可以解决,还有一种是同一个IP无法访问多次,因此我们需要有自己的代理IP池并自动清除和补充代理IP池


一、代理IP的获取

header = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Connection': 'keep-alive',
    'Host': 'www.kuaidaili.com',
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
}
urls = ['https://www.kuaidaili.com/free/inha/','https://www.kuaidaili.com/free/intr/']

def getProxyIp():
    print('开始获取代理IP')
    proxy=[]
    for each_url in urls:
        for i in range(1, 10):
            url = each_url + str(i)+'/'
            html = urllib.request.urlopen(urllib.request.Request(url, headers=header)).read()
            html = gzip.decompress(html).decode("utf-8")
            soup = BeautifulSoup(html,'lxml')
            ips = soup.findAll('tr')
            for x in range(1, len(ips)):
                ip = ips[x]
                tds = ip.findAll("td")
                ip_temp = tds[0].contents[0] + " " + tds[1].contents[0]
                proxy.append(ip_temp)
            time.sleep(3)
    return proxy

二、代理IP的验证

def validateIp(proxy):
    print('开始验证代理IP,以下IP可用')
    url = "http://ip.chinaz.com/getip.aspx"
    socket.setdefaulttimeout(3)
    for i in range(0, len(proxy)):
        try:
            ip = proxy[i].strip().split(" ")
            proxy_host = "http://" + ip[0] + ":" + ip[1]
            proxy_handler = urllib.request.ProxyHandler({"http":proxy_host})
            res = urllib.request.build_opener(proxy_handler).open(url).read()
            print(proxy[i])
        except:
            continue

三、代理IP的使用

import urllib.request
import random
url = 'http://www.whatismyip.com.tw'
iplist=['60.191.164.83:3128','211.108.62.56:80','218.5.238.169:8080','210.136.19.243:8080']
proxy_support = urllib.request.ProxyHandler({'http':'60.191.164.83:3128'})
opener = urllib.request.build_opener(proxy_support)
urllib.request.install_opener(opener)
response = urllib.request.urlopen(url)
html = response.read().decode('utf-8')
print(html)

效果如下:

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱学习的广东仔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值