• 搭建免费代理池---采集代理(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

  • 相关阅读:
    第九届蓝桥杯省赛c/c++真题明码题解答案,另类excel解法思路
    Windows下将Python源代码.py文件封装成exe可执行文件方法
    windows下python自带的pip安装速度过慢解决方案
    解决:Errors were encountered while processing
    ubuntu 安装 caffe 解决://home/xiaojie/anaconda/lib/libpng16.so.16:对‘inflateValidate@ZLIB_1.2.9’未定义的引用
    ubuntu安装caffe 解决:build_release/tools/caffe: error while loading shared libraries: libcudart.so.8.0: cannot open shar
    ubuntu 禁止内核更新
    ubutun 中notebook出现 Permission denied: Untitled.ipynb
    ubuntu下anaconda使用jupyter notebook加载tensorflow、pytorch
    ubuntu下用anaconda快速安装 pytorch
  • 原文地址:https://www.cnblogs.com/whnba/p/11774079.html
Copyright © 2020-2023  润新知