def test_gen_search_gzh_url_page(self, page):
     if page > 0:
         url = WechatSogouRequest.gen_search_gzh_url(gaokao_keyword, page)
         assert_in('page={}'.format(page), url)
     else:
         with assert_raises(AssertionError):
             WechatSogouRequest.gen_search_gzh_url(gaokao_keyword, page)
Пример #2
0
 def test_gen_search_gzh_url_page(self, page):
     if page > 0:
         url = WechatSogouRequest.gen_search_gzh_url(gaokao_keyword, page)
         assert_in('page={}'.format(page), url)
     else:
         with assert_raises(AssertionError):
             WechatSogouRequest.gen_search_gzh_url(gaokao_keyword, page)
Пример #3
0
    def search_gzh(self, keyword, page=1, unlock_callback=None, identify_image_callback=None, decode_url=True):
        """搜索 公众号

        对于出现验证码的情况,可以由使用者自己提供:
            1、函数 unlock_callback ,这个函数 handle 出现验证码到解决的整个流程
            2、也可以 只提供函数 identify_image_callback,这个函数输入验证码二进制数据,输出验证码文字,剩下的由 wechatsogou 包来解决
        注意:
            函数 unlock_callback 和 identify_image_callback 只需要提供一个,如果都提供了,那么 identify_image_callback 不起作用

        Parameters
        ----------
        keyword : str or unicode
            搜索文字
        page : int, optional
            页数 the default is 1
        unlock_callback : callable
            处理出现验证码页面的函数,参见 unlock_callback_example
        identify_image_callback : callable
            处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example
        decode_url : bool
            是否解析 url

        Returns
        -------
        list[dict]
            {
                'open_id': '', # 微信号唯一ID
                'profile_url': '',  # 最近10条群发页链接
                'headimage': '',  # 头像
                'wechat_name': '',  # 名称
                'wechat_id': '',  # 微信id
                'post_perm': '',  # 最近一月群发数
                'qrcode': '',  # 二维码
                'introduction': '',  # 介绍
                'authentication': ''  # 认证
            }

        Raises
        ------
        WechatSogouRequestsException
            requests error
        """
        url = WechatSogouRequest.gen_search_gzh_url(keyword, page)
        session = requests.session()
        resp = self.__get_by_unlock(url,
                                    unlock_platform=self.__unlock_sogou,
                                    unlock_callback=unlock_callback,
                                    identify_image_callback=identify_image_callback,
                                    session=session)
        gzh_list = WechatSogouStructuring.get_gzh_by_search(resp.text)
        for i in gzh_list:
            if decode_url:
                i['profile_url'] = self.__format_url(i['profile_url'], url, resp.text, unlock_callback=unlock_callback, identify_image_callback=identify_image_callback, session=session)
            yield i
Пример #4
0
    def search_gzh(self, keyword, page=1, unlock_callback=None, identify_image_callback=None, decode_url=True):
        """搜索 公众号

        对于出现验证码的情况,可以由使用者自己提供:
            1、函数 unlock_callback ,这个函数 handle 出现验证码到解决的整个流程
            2、也可以 只提供函数 identify_image_callback,这个函数输入验证码二进制数据,输出验证码文字,剩下的由 wechatsogou 包来解决
        注意:
            函数 unlock_callback 和 identify_image_callback 只需要提供一个,如果都提供了,那么 identify_image_callback 不起作用

        Parameters
        ----------
        keyword : str or unicode
            搜索文字
        page : int, optional
            页数 the default is 1
        unlock_callback : callable
            处理出现验证码页面的函数,参见 unlock_callback_example
        identify_image_callback : callable
            处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example
        decode_url : bool
            是否解析 url

        Returns
        -------
        list[dict]
            {
                'open_id': '', # 微信号唯一ID
                'profile_url': '',  # 最近10条群发页链接
                'headimage': '',  # 头像
                'wechat_name': '',  # 名称
                'wechat_id': '',  # 微信id
                'post_perm': '',  # 最近一月群发数
                'qrcode': '',  # 二维码
                'introduction': '',  # 介绍
                'authentication': ''  # 认证
            }

        Raises
        ------
        WechatSogouRequestsException
            requests error
        """
        url = WechatSogouRequest.gen_search_gzh_url(keyword, page)
        session = requests.session()
        resp = self.__get_by_unlock(url,
                                    unlock_platform=self.__unlock_sogou,
                                    unlock_callback=unlock_callback,
                                    identify_image_callback=identify_image_callback,
                                    session=session)
        gzh_list = WechatSogouStructuring.get_gzh_by_search(resp.text)
        for i in gzh_list:
            if decode_url:
                i['profile_url'] = self.__format_url(i['profile_url'], url, resp.text, unlock_callback=unlock_callback, identify_image_callback=identify_image_callback, session=session)
            yield i
Пример #5
0
    def test_search_gzh(self):
        url = WechatSogouRequest.gen_search_gzh_url(gaokao_keyword)
        file_name = '{}/{}/{}'.format(os.getcwd(), fake_data_path,
                                      'search-gaokao-gzh.html')
        with io.open(file_name, encoding='utf-8') as f:
            search_gaokao_gzh_error = f.read()
            httpretty.register_uri(httpretty.GET,
                                   url,
                                   body=search_gaokao_gzh_error)

        gzh_list = ws_api.search_gzh(gaokao_keyword)
        assert_equal(10, len(gzh_list))
        assert_equal([
            '山东高考指南', '高考家长圈', '河南高考指南', '高考360', '云天高考', '腾讯高考', '高考快讯',
            '专业中高考教育', '晟嘉高考', '新东方在线高考辅导'
        ], [i['wechat_name'] for i in gzh_list])
 def test_gen_search_article_url_keyword(self):
     url = WechatSogouRequest.gen_search_gzh_url(gaokao_keyword)
     assert_equal('http://weixin.sogou.com/weixin?type=1&page=1&ie=utf8&query=%E9%AB%98%E8%80%83', url)
Пример #7
0
 def test_gen_search_article_url_keyword(self):
     url = WechatSogouRequest.gen_search_gzh_url(gaokao_keyword)
     assert_equal(
         'http://weixin.sogou.com/weixin?type=1&page=1&ie=utf8&query=%E9%AB%98%E8%80%83',
         url)
Пример #8
0
    def search_gzh(self,
                   keyword,
                   page=1,
                   deblocking_callback=None,
                   identify_image_callback=None):
        """搜索 公众号

        对于出现验证码的情况,可以由使用者自己提供:
            1、函数 deblocking_callback ,这个函数 handle 出现验证码到解决的整个流程
            2、也可以 只提供函数 identify_image_callback,这个函数输入验证码二进制数据,输出验证码文字,剩下的由 wechatsogou 包来解决
        注意:
            函数 deblocking_callback 和 identify_image_callback 只需要提供一个,如果都提供了,那么 identify_image_callback 不起作用

        Parameters
        ----------
        keyword : str or unicode
            搜索文字
        page : int, optional
            页数 the default is 1
        deblocking_callback : callable
            处理出现验证码页面的函数,参见 deblocking_callback_example
        identify_image_callback : callable
            处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example

        Returns
        -------
        list[dict]
            {
                'url': '',
                'img': '',
                'name': '',
                'wechat_id': '',
                'post_perm': '',
                'qrcode': '',
                'introduction': '',
                'authentication': ''
            }

        Raises
        ------
        WechatSogouRequestsException
            requests error
        """
        req = requests.session()

        url = WechatSogouRequest.gen_search_gzh_url(keyword, page)
        resp = WechatSogouRequest.get(url,
                                      req=req,
                                      headers=self.__set_cookie())

        if not resp.ok:
            raise WechatSogouRequestsException('WechatSogouAPI search_gzh',
                                               resp)

        if 'antispider' in resp.url:
            self.__deblocking_search(url, resp, req, deblocking_callback,
                                     identify_image_callback)
            resp = WechatSogouRequest.get(
                url, req=req, headers=self.__set_cookie())  # req=req

        return WechatSogouStructuring.get_gzh_by_search(resp.text)