[18072] 2020-05-10_我用Python爬了微信好友,原来大家的人生态度是这样……

文档创建者:s7ckTeam
浏览次数:4
最后更新:2025-01-18
2020-05-10_我用Python爬了微信好友,原来大家的人生态度是这样…… P y t h o n   H A C K   2 0 2 0 - 0 5 - 1 0 使 P y t h o n 使 i t c h a t P y t h o n *   j i e b a   P y t h o n   *   m a t p l o t l i b P y t h o n   *   s n o w n l p   P y t h o n   *   P I L P y t h o n   *   n u m p y P y t h o n     w o r d c l o u d   使 *   w o r d c l o u d P y t h o n   *   T e n c e n t Y o u t u y u n   P y t h o n     S D K     p i p   使 0 1 使   i t c h a t   使 f r i e n d s f r i e n d s [ 1 : ] S e x C i t y P r o v i n c e H e a d I m g U r l S i g n a t u r e 0 2 S e x M a l e F e m a l e U n k o n w 使 m a t p l o t l i b i t c h a t . a u t o _ l o g i n ( h o t R e l o a d   =   T r u e )   f r i e n d s   =   i t c h a t . g e t _ f r i e n d s ( u p d a t e   =   T r u e )
U n k o n w M a l e F e m a l e 0 1 2 C o l l e c t i o n C o u n t e r ( ) i t e m s ( ) 0 1 2 0 1 2   m a p ( ) m a t p l o t l i b m a t p l o t l i b m a t p l o t l i b 0 3 使 H e a d I m g U r l A P I 使 使 使 使 d e f   a n a l y s e S e x ( f i r e n d s ) :     s e x s   =   l i s t ( m a p ( l a m b d a   x : x [ ' S e x ' ] , f r i e n d s [ 1 : ] ) )     c o u n t s   =   l i s t ( m a p ( l a m b d a   x : x [ 1 ] , C o u n t e r ( s e x s ) . i t e m s ( ) ) )     l a b e l s   =   [ ' U n k n o w ' , ' M a l e ' , ' F e m a l e ' ]     c o l o r s   =   [ ' r e d ' , ' y e l l o w g r e e n ' , ' l i g h t s k y b l u e ' ]     p l t . f i g u r e ( f i g s i z e = ( 8 , 5 ) ,   d p i = 8 0 )     p l t . a x e s ( a s p e c t = 1 )     p l t . p i e ( c o u n t s ,   #         l a b e l s = l a b e l s ,   #         c o l o r s = c o l o r s ,   #         l a b e l d i s t a n c e   =   1 . 1 ,   #         a u t o p c t   =   ' % 3 . 1 f % % ' ,   #         s h a d o w   =   F a l s e ,   #         s t a r t a n g l e   =   9 0 ,   #         p c t d i s t a n c e   =   0 . 6   #     )     p l t . l e g e n d ( l o c = ' u p p e r   r i g h t ' , )     p l t . t i t l e ( u ' % s '   %   f r i e n d s [ 0 ] [ ' N i c k N a m e ' ] )     p l t . s h o w ( )
d e f   a n a l y s e H e a d I m a g e ( f r i n e d s ) : #   I n i t   P a t h     b a s e P a t h   =   o s . p a t h . a b s p a t h ( ' . ' )     b a s e F o l d e r   =   b a s e P a t h   +   ' H e a d I m a g e s ' i f ( o s . p a t h . e x i s t s ( b a s e F o l d e r )   = =   F a l s e ) :       o s . m a k e d i r s ( b a s e F o l d e r )     #   A n a l y s e   I m a g e s     f a c e A p i   =   F a c e A P I ( )     u s e _ f a c e   =   0   n o t _ u s e _ f a c e   =   0   i m a g e _ t a g s   =   ' ' f o r   i n d e x   i n   r a n g e ( 1 , l e n ( f r i e n d s ) ) :       f r i e n d   =   f r i e n d s [ i n d e x ]   #   S a v e   H e a d I m a g e s       i m g F i l e   =   b a s e F o l d e r   +   ' I m a g e % s . j p g '   %   s t r ( i n d e x )       i m g D a t a   =   i t c h a t . g e t _ h e a d _ i m g ( u s e r N a m e   =   f r i e n d [ ' U s e r N a m e ' ] )   i f ( o s . p a t h . e x i s t s ( i m g F i l e )   = =   F a l s e ) :   w i t h   o p e n ( i m g F i l e , ' w b ' )   a s   f i l e :           f i l e . w r i t e ( i m g D a t a )       #   D e t e c t   F a c e s       t i m e . s l e e p ( 1 )       r e s u l t   =   f a c e A p i . d e t e c t F a c e ( i m g F i l e )   i f   r e s u l t   = =   T r u e :         u s e _ f a c e   + =   1 e l s e :         n o t _ u s e _ f a c e   + =   1 #   E x t r a c t   T a g s       r e s u l t   =   f a c e A p i . e x t r a c t T a g s ( i m g F i l e )       i m a g e _ t a g s   + =   ' , ' . j o i n ( l i s t ( m a p ( l a m b d a   x : x [ ' t a g _ n a m e ' ] , r e s u l t ) ) )       l a b e l s   =   [ u ' 使 ' , u ' 使 ' ]     c o u n t s   =   [ u s e _ f a c e , n o t _ u s e _ f a c e ]     c o l o r s   =   [ ' r e d ' , ' y e l l o w g r e e n ' , ' l i g h t s k y b l u e ' ]     p l t . f i g u r e ( f i g s i z e = ( 8 , 5 ) ,   d p i = 8 0 )     p l t . a x e s ( a s p e c t = 1 )     p l t . p i e ( c o u n t s ,   #         l a b e l s = l a b e l s ,   #         c o l o r s = c o l o r s ,   #         l a b e l d i s t a n c e   =   1 . 1 ,   #         a u t o p c t   =   ' % 3 . 1 f % % ' ,   #         s h a d o w   =   F a l s e ,   #  
H e a d I m a g e s F a c e A p i S D K A P I 使 使 1 / 4 使   3 / 4   2 5 % 7 5 %       s h a d o w   =   F a l s e ,   #         s t a r t a n g l e   =   9 0 ,   #         p c t d i s t a n c e   =   0 . 6   #     )     p l t . l e g e n d ( l o c = ' u p p e r   r i g h t ' , )     p l t . t i t l e ( u ' % s 使 '   %   f r i e n d s [ 0 ] [ ' N i c k N a m e ' ] )     p l t . s h o w ( )       i m a g e _ t a g s   =   i m a g e _ t a g s . e n c o d e ( ' i s o 8 8 5 9 - 1 ' ) . d e c o d e ( ' u t f - 8 ' )     b a c k _ c o l o r i n g   =   n p . a r r a y ( I m a g e . o p e n ( ' f a c e . j p g ' ) )     w o r d c l o u d   =   W o r d C l o u d (       f o n t _ p a t h = ' s i m f a n g . t t f ' ,       b a c k g r o u n d _ c o l o r = " w h i t e " ,       m a x _ w o r d s = 1 2 0 0 ,       m a s k = b a c k _ c o l o r i n g ,       m a x _ f o n t _ s i z e = 7 5 ,       r a n d o m _ s t a t e = 4 5 ,       w i d t h = 8 0 0 ,       h e i g h t = 4 8 0 ,       m a r g i n = 1 5   )       w o r d c l o u d . g e n e r a t e ( i m a g e _ t a g s )     p l t . i m s h o w ( w o r d c l o u d )     p l t . a x i s ( " o f f " )     p l t . s h o w ( )
使 1 5 使 5 3 使 2 5 使 3 使 5 使 1 3 使 1 8 0 4 使 使 使 使 S n o w N L P S i g n a t u r e d e f   a n a l y s e S i g n a t u r e ( f r i e n d s ) :   s i g n a t u r e s   =   ' '   e m o t i o n s   =   [ ]     p a t t e r n   =   r e . c o m p i l e ( " 1 f d . + " )   f o r   f r i e n d   i n   f r i e n d s :       s i g n a t u r e   =   f r i e n d [ ' S i g n a t u r e ' ]   i f ( s i g n a t u r e   ! =   N o n e ) :         s i g n a t u r e   =   s i g n a t u r e . s t r i p ( ) . r e p l a c e ( ' s p a n ' ,   ' ' ) . r e p l a c e ( ' c l a s s ' ,   ' ' ) . r e p l a c e ( ' e m o j i ' ,   ' ' )         s i g n a t u r e   =   r e . s u b ( r ' 1 f ( d . + ) ' , ' ' , s i g n a t u r e )   i f ( l e n ( s i g n a t u r e ) > 0 ) :           n l p   =   S n o w N L P ( s i g n a t u r e )           e m o t i o n s . a p p e n d ( n l p . s e n t i m e n t s )           s i g n a t u r e s   + =   '   ' . j o i n ( j i e b a . a n a l y s e . e x t r a c t _ t a g s ( s i g n a t u r e , 5 ) )   w i t h   o p e n ( ' s i g n a t u r e s . t x t ' , ' w t ' , e n c o d i n g = ' u t f - 8 ' )   a s   f i l e :         f i l e . w r i t e ( s i g n a t u r e s )   #   S i n a t u r e   W o r d C l o u d     b a c k _ c o l o r i n g   =   n p . a r r a y ( I m a g e . o p e n ( ' f l o w e r . j p g ' ) )  
  w o r d c l o u d   =   W o r d C l o u d (       f o n t _ p a t h = ' s i m f a n g . t t f ' ,       b a c k g r o u n d _ c o l o r = " w h i t e " ,       m a x _ w o r d s = 1 2 0 0 ,       m a s k = b a c k _ c o l o r i n g ,       m a x _ f o n t _ s i z e = 7 5 ,       r a n d o m _ s t a t e = 4 5 ,       w i d t h = 9 6 0 ,       h e i g h t = 7 2 0 ,       m a r g i n = 1 5   )     w o r d c l o u d . g e n e r a t e ( s i g n a t u r e s )     p l t . i m s h o w ( w o r d c l o u d )     p l t . a x i s ( " o f f " )     p l t . s h o w ( )     w o r d c l o u d . t o _ f i l e ( ' s i g n a t u r e s . j p g ' )   #   S i g n a t u r e   E m o t i o n a l   J u d g m e n t     c o u n t _ g o o d   =   l e n ( l i s t ( f i l t e r ( l a m b d a   x : x > 0 . 6 6 , e m o t i o n s ) ) )     c o u n t _ n o r m a l   =   l e n ( l i s t ( f i l t e r ( l a m b d a   x : x > = 0 . 3 3   a n d   x < = 0 . 6 6 , e m o t i o n s ) ) )     c o u n t _ b a d   =   l e n ( l i s t ( f i l t e r ( l a m b d a   x : x < 0 . 3 3 , e m o t i o n s ) ) )     l a b e l s   =   [ u ' ' , u ' ' , u ' ' ]     v a l u e s   =   ( c o u n t _ b a d , c o u n t _ n o r m a l , c o u n t _ g o o d )     p l t . r c P a r a m s [ ' f o n t . s a n s - s e r i f ' ]   =   [ ' s i m H e i ' ]     p l t . r c P a r a m s [ ' a x e s . u n i c o d e _ m i n u s ' ]   =   F a l s e   p l t . x l a b e l ( u ' ' )     p l t . y l a b e l ( u ' ' )     p l t . x t i c k s ( r a n g e ( 3 ) , l a b e l s )     p l t . l e g e n d ( l o c = ' u p p e r   r i g h t ' , )     p l t . b a r ( r a n g e ( 3 ) ,   v a l u e s ,   c o l o r   =   ' r g b ' )     p l t . t i t l e ( u ' % s '   %   f r i e n d s [ 0 ] [ ' N i c k N a m e ' ] )     p l t . s h o w ( )
5 5 . 5 6 % 3 2 . 1 0 % 1 2 . 3 5 % 8 7 . 6 6 % 0 5 P r o v i n c e C i t y P y t h o n B a s e m a p 使 便 E C h a r t s 使 p y e c h a r t s E c h a r t s J S O N 使 使 B D P P y t h o n C S V B D P C S V B D P 西 d e f   a n a l y s e L o c a t i o n ( f r i e n d s ) :     h e a d e r s   =   [ ' N i c k N a m e ' , ' P r o v i n c e ' , ' C i t y ' ]   w i t h   o p e n ( ' l o c a t i o n . c s v ' , ' w ' , e n c o d i n g = ' u t f - 8 ' , n e w l i n e = ' ' , )   a s   c s v F i l e :       w r i t e r   =   c s v . D i c t W r i t e r ( c s v F i l e ,   h e a d e r s )       w r i t e r . w r i t e h e a d e r ( )   f o r   f r i e n d   i n   f r i e n d s [ 1 : ] :   r o w   =   { }   r o w [ ' N i c k N a m e ' ]   =   f r i e n d [ ' N i c k N a m e ' ]   r o w [ ' P r o v i n c e ' ]   =   f r i e n d [ ' P r o v i n c e ' ]   r o w [ ' C i t y ' ]   =   f r i e n d [ ' C i t y ' ]         w r i t e r . w r i t e r o w ( r o w )
0 6 E n d . 鸿
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则