搭建代理ip池

在使用爬虫时,很容易碰到被封ip的情况。遇到这种情况,就需要使用代理ip了。

思路:

1:爬取提供代理ip的网站

2:检测爬取到的ip是否可用

3:将可用的ip存入数据库(同时检测数据库中是否已存在该ip)

4:调用接口,从数据库中获取ip(同时检查数据库ip的数量,若数量小于5条,就重复以上步骤)

5:若获取到的ip不可用,则重新获取,并将不可用的ip从数据库中删除

实现:

1:爬取提供代理ip的网站

class  ProiexsPool:
    @staticmethod
    def _get_proiexs():
        # 存放所有ip的列表
        ips = [];
        # 首先爬取代理ip的网站(只爬取前两页)
        for i in range(1,3):
            html = requests.get("https://www.kuaidaili.com/free/inha/"+str(i)+"/",headers={
                "User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:52.0) Gecko/20100101 Firefox/52.0"
            }).text;
            # 取出ip和协议类型
            html_tree = etree.HTML(html);
            tr_ips = html_tree.xpath("//tbody/tr");
            # print(tr_ips);
            for index,tr in enumerate(tr_ips):
                xieyi = tr.xpath("//td[4]/text()")[index];
                ip = tr.xpath("//td[1]/text()")[index]+":"+tr.xpath("//td[2]/text()")[index];
                # print(type(ip));
                # port = ;
                full_ip = {xieyi : ip};
                # 测试ip是否可用
                if ProiexsPool._check_ip(full_ip):
                    # 查看数据库中是否存在该组ip
                    if ProiexsPool._select_ip_from_database(ip):
                        # 将所有可用的ip存入数据库
                        ProiexsPool._save_ip_to_database(xieyi,ip);
            if i == 2:
                break;
            else:
                time.sleep(5);

2:检测爬取到的ip是否可用

 @staticmethod
    def _check_ip(ip):
        """
        检查ip是否可用的方法
        :param ip:要检查的ip
        :return: True或者False
        """
        try:
            # print("正在检查ip:{}".format(ip));
            html = requests.get("http://bj.58.com/chuzu/?PGTID=0d100000-0000-15df-5f6f-34fb3bfd7994&ClickID=3",headers={
                "User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:52.0) Gecko/20100101 Firefox/52.0"
            },proxies=ip,timeout = 3);
        except Exception as e:
            print(e);
            return False;
        else:
            if html.status_code == 200:
                return True;
            else:
                return False;

3:检测数据库中是否存在该组ip

@staticmethod
    def _select_ip_from_database(ip):
        """
        根据传入的ip查询,看数据库中是否有该ip,决定是否保存该ip
        :param ip: 要查询的ip
        :return: True或False
        """
        try:
            conn = sqlite3.connect("ProxiesPool.db");
            cursor = conn.cursor();
            cursor.execute("create table if not exists proxies(xieyi varchar,ip varchar)");
            count = cursor.execute("select count(ip) from proxies where ip = '{}'".format(ip));
            # print(count);
            if not count.__next__()[0]:
                return True;
            else:
                return False;
        except Exception as e:
            print(e);
        finally:
            conn.commit();
            cursor.close();
            conn.close();

4:若数据库中不存在某组ip,就将该ip存入数据库

 @staticmethod
    def _save_ip_to_database(xieyi,ip):
        """
        将ip存入数据库
        :param xieyi: 代理ip的协议(https,http,socket等)
        :param ip: 一个ip代理(字典类型)
        :return: None
        """
        conn = sqlite3.connect("ProxiesPool.db");
        cursor = conn.cursor();
        cursor.execute("create table if not exists proxies(xieyi varchar,ip varchar)");
        cursor.execute("insert into proxies(xieyi ,ip) values ('{}','{}')".format(xieyi,ip));
        conn.commit();
        cursor.close();
        conn.close();

5:从数据库中随机获得一个代理ip供用户使用

@staticmethod
    def _get_random_ip():
        """
        随机获得一个ip,并检测数据库的ip数量
        :return: None
        """
        conn = sqlite3.connect("ProxiesPool.db");
        cursor = conn.cursor();
        cursor.execute("create table if not exists proxies(xieyi varchar,ip varchar)");
        counts = cursor.execute("select count(*) from proxies")
        if counts.__next__()[0] < 5:
            # 如果数据库里的ip数量小于5个,则往数据库中重新填入数据
            ProiexsPool._get_proiexs();
        # 获得数据库中所有Ip
        proxies = cursor.execute("select * from proxies");
        ips = [];
        for xieyi,ip in proxies:
            ips.append({xieyi : xieyi.lower()+"://"+ip});
        conn.commit();
        cursor.close();
        conn.close();
        return random.choice(ips);

6:若从数据库中随机获取到的ip不能使用,则将其从数据库中删除

@staticmethod
    def _delete_ip_from_db(ip):
        """
        根据传入的ip删除失效的ip数据
        :param ip: 要删除的ip
        :return: None
        """
        conn = sqlite3.connect("ProxiesPool.db");
        cursor = conn.cursor();
        cursor.execute("create table if not exists proxies(xieyi varchar,ip varchar)");
        cursor.execute("delete from proxies where ip = '{}'".format(ip));
        conn.commit();
        cursor.close();
        conn.close();
        print("成功删除失效代理ip:{}.....".format(ip));

完整的代码可参考我的GitHub:https://github.com/shyorange/CommonlyUsedToolsProjects/blob/master/Proiexs_Pool.py

使用方法:

from Proiexs_Pool import ProiexsPool

pool = ProiexsPool();
pool._get_proiexs();
ip = pool._get_random_ip()
print(ip);

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
搭建Selenium IP代理,你可以按照以下步骤进行操作: 1. 首先,你需要安装selenium-wire模块。你可以使用以下命令在终端中安装该模块: ```pip install selenium-wire``` 2. 接下来,你需要导入所需的包和模块。例如,你可以导入selenium、fake_useragent和selenium.webdriver.chrome.options模块。 ``` from selenium import webdriver from fake_useragent import UserAgent from selenium.webdriver.chrome.options import Options ``` 3. 设置代理服务器的配置和用户代理头。你可以使用ChromeOptions()来设置代理服务器和添加用户代理头。 ``` ops = Options() headers = {'User-Agent': UserAgent().random} ``` 4. 创建一个WebDriver实例,并使用指定的驱动程序路径来初始化。例如,你可以使用webdriver.Chrome()来创建一个Chrome浏览器的实例。 ``` driver = webdriver.Chrome(r'D:\360安全浏览器下载\chromedriver.exe') ``` 5. 获取代理IP地址并将其添加到代理服务器中。你可以使用webdriver的get()方法来打开代理IP地址的网页,并使用find_element_by_xpath()方法来获取代理IP地址的文本。然后,你可以使用add_argument()方法将代理IP地址添加到ChromeOptions中的代理服务器选项中。 ``` api_url = '让你复制的代理api链接' driver.get(api_url) a = driver.find_element_by_xpath('/html/body/pre').text # 获取代理 ops.add_argument('--proxy-server=http://%s' % a) # 添加代理 ``` 6. 清除浏览器的cookies。你可以使用delete_all_cookies()方法来清除浏览器的cookies。 ``` driver.delete_all_cookies() # 清除cookies ``` 7. 打开你想要使用代理IP访问的网页,并执行你需要的操作。例如,你可以使用get()方法打开淘宝网并在搜索框中输入关键字。 ``` driver.get('https://www.taobao.com/') driver.find_element_by_name('q').send_keys('华为手机') ``` 请注意,上述代码仅供参考,你需要根据自己的实际情况进行适当的修改和调整。确保你已经正确安装了selenium-wire模块,并且已经下载和配置了Chrome驱动程序。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [selenium使用代理IP](https://blog.csdn.net/weixin_46211269/article/details/123251070)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值