zoukankan      html  css  js  c++  java
  • 搭建免费代理池---采集代理(1)

    在对网站信息进去抓取时,难免遇到被封IP的情况。针对这一情况可采用代理IP的方法来处理,好了  现在我遇到的问题是什么呢? 就是我没有代理IP啊。

    百度了下,发现网上有好多免费的代理IP,所以我决定把能找到的所以免费代理IP全部采集下来,以后做成接口的方式来供大家免费使用。

    本篇文章主要是对采集 “66免费代理网 http://www.66ip.cn/”做技术总结。

    1、GET/POST 请求

    为了让操作更加简单 采用工厂模式对GET / Post 请求进行了封装。

    import requests
    import abc
    
    '''
    请求方法抽象类
    '''
    
    
    class AbsMethod:
    
        @abc.abstractmethod
        def request(self, url, attach):
            pass
    
    
    '''
    Get 方法
    '''
    
    
    class Get(AbsMethod):
        '''
        请求
        '''
    
        def request(self, url, attach) -> requests.Response:
            res = requests.post(url, attach)
            if not res.ok:
                return res.raise_for_status()
            return res
    
    
    '''
    Post 方法
    '''
    
    
    class Post(AbsMethod):
        '''
        请求
        '''
    
        def request(self, url, attach) -> requests.Response:
            res = requests.get(url, attach)
            if not res.ok:
                return res.raise_for_status()
            return res
    
    
    '''
    方法工厂
    '''
    
    
    class MethodFactory:
        def create(self, method: str) -> AbsMethod:
            return eval(method)()
    
    
    '''
    http 请求
    '''
    
    
    class HttpReuqest:
    
        '''
        发送求请
        '''
        @staticmethod
        def send(url, attach = {}, method='Get') -> requests.Response:
            factory = MethodFactory()
            target = factory.create(method)
            return target.request(url, attach)

    2、采集目标站点

    class WWW_66IP_CN:
    
        '''
        URL地址
        '''
        __url = 'http://www.66ip.cn'
    
        '''
        页面编码
        '''
        __code = 'gbk'
    
        '''
        选择器
        '''
        __selector = '.containerbox table tr'
    
        '''
        获取免费代理
        '''
    
        def get_proxy(self) -> str:
            soup = bs4.BeautifulSoup(self.text, 'lxml')
            result = soup.select(self.__selector)
            result = self.__filters([str(n) for n in result])
            return result
    
        '''
        获取页面内容
        '''
        @property
        def text(self) -> str:
            http = HttpReuqest()
            res = http.send(self.__url)
            if res.headers['Content-Encoding'] == 'gzip':  # 页面采用gizp压缩了,需要对它进行解码不然中文会乱码
                return res.content.decode(self.__code)
            return res.text
    
        '''
        过滤
        '''
    
        def __filters(self, items: List[str]) -> List[list]:
            result, regex = [], re.compile(r'<td>([^<>]+)</td>')
            for item in items:
                result.append(regex.findall(item))
            return result
    
    
    proxy = WWW_66IP_CN()
    
    d = proxy.get_proxy()
    
    print(d)

    3、技术总结

      目标站点采用了 gzip 进行了页面压缩,如果不对页面进行解码那么中文字符就会以乱码的形式出现。针对这一情况,可采用 字符串函数 decode()进行解码

      

      

    4、百度网盘

    链接:https://pan.baidu.com/s/1BStzSFPteMCcfOum6_4RUw
    提取码:dlsr

  • 相关阅读:
    ASP.NET编程的十大技巧
    C#学习心得(转)
    POJ 1177 Picture (线段树)
    POJ 3067 Japan (树状数组)
    POJ 2828 Buy Tickets (线段树)
    POJ 1195 Mobile phones (二维树状数组)
    HDU 4235 Flowers (线段树)
    POJ 2886 Who Gets the Most Candies? (线段树)
    POJ 2418 Cows (树状数组)
    HDU 4339 Query (线段树)
  • 原文地址:https://www.cnblogs.com/whnba/p/11774079.html
Copyright © 2011-2022 走看看