您當前的位置:首頁 > 動漫

Python抓取微信好友數量、性別、城市分佈,以及將py檔案打包成exe.

作者:由 不知名資料狗 發表于 動漫時間:2019-07-12

import

itchat

import

pandas

as

pd

# 先登入

itchat

login

()

# 獲取好友列表

friends

=

itchat

get_friends

update

=

True

)[

0

:]

#number_friends = itchat。get_friends(update=True)

number_of_friends

=

len

friends

# 初始化計數器,有男有女,當然,有些人是不填的

male

=

female

=

other

=

0

# 遍歷這個列表,列表裡第一位是自己,所以從“自己”之後開始計算

# 1表示男性,2女性

for

i

in

friends

1

:]:

sex

=

i

“Sex”

if

sex

==

1

male

+=

1

elif

sex

==

2

female

+=

1

else

other

+=

1

# 總數算上,好計算比例啊~

total

=

len

friends

1

:])

#好友城市分佈

df_friends

=

pd

DataFrame

friends

City

=

df_friends

City

City_count

=

City

value_counts

()

City_count

=

City_count

City_count

index

!=

‘’

# 好了,列印結果

friends_numbers

=

str

number_of_friends

man

=

u

“男性好友:

%。2f%%

%

float

male

/

total

*

100

))

women

=

u

“女性好友:

%。2f%%

%

float

female

/

total

*

100

))

others

=

u

“其他:

%。2f%%

%

float

other

/

total

*

100

))

city

=

City_count

with

open

‘wechat。txt’

‘w’

as

f

f

write

friends_numbers

f

write

\n

+

man

f

write

women

f

write

\n

+

str

city

))

執行以上程式碼,會彈出一個登入二維碼,掃描執行之後完成。

Python抓取微信好友數量、性別、城市分佈,以及將py檔案打包成exe.

執行完成會生成一個txt檔案:

Python抓取微信好友數量、性別、城市分佈,以及將py檔案打包成exe.

為了方便下次執行,可以將Python檔案打包成exe,執行下面操作即可:

首先原檔案的內容是這樣的

Python抓取微信好友數量、性別、城市分佈,以及將py檔案打包成exe.

然後直接進入cmd視窗節目,切換到Python檔案的目錄

執行pyinstaller -w -F wechat。py進行打包

Python抓取微信好友數量、性別、城市分佈,以及將py檔案打包成exe.

以下提示表示成功

Python抓取微信好友數量、性別、城市分佈,以及將py檔案打包成exe.

可以看看原來的資料夾會多出三個檔案下以及一個txt檔案,打包的exe檔案則放在dist資料夾裡面

Python抓取微信好友數量、性別、城市分佈,以及將py檔案打包成exe.

直接點選執行即可。

Python抓取微信好友數量、性別、城市分佈,以及將py檔案打包成exe.

標簽: Friends  city  total  count  write