Exemplo n.º 1
0
    def get_article_content(self,
                            url,
                            del_qqmusic=True,
                            del_mpvoice=True,
                            unlock_callback=None,
                            identify_image_callback=None,
                            hosting_callback=None,
                            raw=False):
        """获取文章原文,避免临时链接失效

        Parameters
        ----------
        url : str or unicode
            原文链接,临时链接
        raw : bool
            True: 返回原始html
            False: 返回处理后的html
        del_qqmusic: bool
            True:微信原文中有插入的qq音乐,则删除
            False:微信源文中有插入的qq音乐,则保留
        del_mpvoice: bool
            True:微信原文中有插入的语音消息,则删除
            False:微信源文中有插入的语音消息,则保留
        unlock_callback : callable
            处理 文章明细 的时候出现验证码的函数,参见 unlock_callback_example
        identify_image_callback : callable
            处理 文章明细 的时候处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example
        hosting_callback: callable
            将微信采集的文章托管到7牛或者阿里云回调函数,输入微信图片源地址,返回托管后地址

        Returns
        -------
        content_html
            原文内容
        content_img_list
            文章中图片列表

        Raises
        ------
        WechatSogouRequestsException
        """

        resp = self.__get_by_unlock(
            url,
            unlock_platform=self.__unlock_wechat,
            unlock_callback=unlock_callback,
            identify_image_callback=identify_image_callback)

        resp.encoding = 'utf-8'
        if '链接已过期' in resp.text:
            raise WechatSogouException(
                'get_article_content 链接 [{}] 已过期'.format(url))
        if raw:
            return resp.text
        content_info = WechatSogouStructuring.get_article_detail(
            resp.text, del_qqmusic=del_qqmusic, del_voice=del_mpvoice)
        if hosting_callback:
            content_info = self.__hosting_wechat_img(content_info,
                                                     hosting_callback)
        return content_info
Exemplo n.º 2
0
    def get_article_detail(text, del_qqmusic=True, del_voice=True):
        """根据微信文章的临时链接获取明细

        1. 获取文本中所有的图片链接列表
        2. 获取微信文章的html内容页面(去除标题等信息)

        Parameters
        ----------
        text : str or unicode
            一篇微信文章的文本
        del_qqmusic: bool
            删除文章中的qq音乐
        del_voice: bool
            删除文章中的语音内容

        Returns
        -------
        dict
        {
            'content_html': str # 微信文本内容
            'content_img_list': list[img_url1, img_url2, ...] # 微信文本中图片列表

        }
        """
        # 1. 获取微信文本content
        html_obj = BeautifulSoup(text, "lxml")
        content_text = html_obj.find('div', {
            'class': 'rich_media_content',
            'id': 'js_content'
        })

        # 2. 删除部分标签
        if del_qqmusic:
            qqmusic = content_text.find_all('qqmusic') or []
            for music in qqmusic:
                music.parent.decompose()

        if del_voice:
            # voice是一个p标签下的mpvoice标签以及class为'js_audio_frame db'的span构成,所以将父标签删除
            voices = content_text.find_all('mpvoice') or []
            for voice in voices:
                voice.parent.decompose()

        # 3. 获取所有的图片 [img标签,和style中的background-image]
        all_img_set = set()
        all_img_element = content_text.find_all('img') or []
        for ele in all_img_element:
            # 删除部分属性
            img_url = ele.attrs['data-src']
            del ele.attrs['data-src']

            if img_url.startswith('//'):
                img_url = 'http:{}'.format(img_url)

            ele.attrs['src'] = img_url

            if not img_url.startswith('http'):
                raise WechatSogouException('img_url [{}] 不合法'.format(img_url))
            all_img_set.add(img_url)

        backgroud_image = content_text.find_all(
            style=re.compile("background-image")) or []
        for ele in backgroud_image:
            # 删除部分属性
            if ele.attrs.get('data-src'):
                del ele.attrs['data-src']

            if ele.attrs.get('data-wxurl'):
                del ele.attrs['data-wxurl']
            img_url = re.findall(backgroud_image_p, str(ele))
            if not img_url:
                continue
            all_img_set.add(img_url[0])

        # 4. 处理iframe
        all_img_element = content_text.find_all('iframe') or []
        for ele in all_img_element:
            # 删除部分属性
            img_url = ele.attrs['data-src']
            del ele.attrs['data-src']
            ele.attrs['src'] = img_url

        # 5. 返回数据
        all_img_list = list(all_img_set)
        content_html = content_text.prettify()
        # 去除div[id=js_content]
        content_html = re.findall(js_content, content_html)[0][0]
        return {'content_html': content_html, 'content_img_list': all_img_list}
Exemplo n.º 3
0
 def new_setattr(self, name, value):
     raise WechatSogouException(
         'const : {} can not be changed'.format(name))