Here comes itchat

2021-02-06 Pyclub編程社

首先我們需要引入這個庫:

import itchat

然後,我們需要登陸自己的微信帳號。此時我們使用的是網頁版微信帳號:

itchat.autologin()

這行會彈出一個二維碼,只需用微信掃一掃然後手機登陸就行啦。

登陸微信了之後第一件事是什麼?!

發微信呀。

所以我們先向文件傳輸助手發個信息吧!

itchat.send('Hello Python', toUserName='filehelper')

我的文件傳輸助手收到了「Hello Python」 呢!

突然想搞事情。嘿嘿。

怎麼實現最簡單的自動回復呢?

itchat中有一些非常使用的裝飾符(decorator)

他們可以用來修飾你的自定義函數

比如我現在要用的:

@itchat.msg_register(itchat.content.TEXT)

這幫助我們找到「Text」格式的回覆

所以我們用這個裝飾符來裝飾自動定義函數:

@itchat.msg_register(itchat.content.TEXT)

def text_reply(msg):

    return msg.text

然後我們登陸自己的微信:

itchat.auto_login()

並且執行這個函數:

itchat.run()

 現在。。。

你的微信就行一個複讀機一樣,一直複製對方說的話並回去了。

我們可以通過類似的方式讀取微信帳號:

@itchat.msg_register(TEXT)

def _(msg):

    # equals to print(msg['FromUserName'])

    print(msg.fromUserName)

它會print出。。。。

@8937482749832798428(一坨東西)

這就是他的微信名字啦

我們來從好友列表裡搜索他。

author = itchat.search_friends(Remarkname='你給他的備註')[0]

author.send('Hello!')

我們就成功的回消息啦

我們還可以下載附件

@itchat.msg_register(ATTACHMENT)

def download_files(msg):
   msg['Text'](msg['FileName'])

所以我們可以把需要下載的附件都寫進來:

@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])

def download_files(msg):    

msg['Text'](msg['FileName'])   

     return '@%s@%s' % ({'Picture': 'img', 'Video': 'vid'}.get(msg['Type'], 'fil'), msg['FileName'])

然後同學們就可以舉一反三

下面的圖是itchat可以訪問的文件類型和text的鍵值內容:

相關焦點

  • 英文歌曲||Beatles經典 太陽出來了 Here Comes The Sun
    Little darling,It feels like years since it’s been here.小寶貝,這是一個漫長孤寂的寒冬。小寶貝,那寒冬仿佛已停留多年。Here comes the sun.Here comes the sun.
  • 英語繪本故事 When Spring Comes
    英語繪本故事 When Spring ComesBefore Spring comes,the trees look like black sticks against the sky.如果你等待,春天會把剩下的雪堆變小越來越小,越來越小,然後消失Before Spring comes, the grass is brown.But if you wait, spring will turn it green and add little flowers春天來之前,草是棕色的。
  • 新歌速遞 | 高爾宣 - Till My Ride Comes - 電影<複身犯>主題曲
    :史科特音樂股份有限公司作詞:高爾宣 OSN / Skot Suyama 陶山作曲:高爾宣 OSN / Skot Suyama 陶山 編曲:Skot Suyama 陶山I keep chasing dreams but I don’t know what they meanI’m stuck in betweenTill my ride comes
  • When It Comes To You
    piece 我已厭倦當個反面角色You been givin' me a bad time 你一直讓我過的很糟糕Tell me what'd I do 告訴我 我該做什麼How come I always get a hard time 我要怎麼才能捱過這段艱難的時光Honey when it comes
  • 歌曲名:Here With Me
    >你將活在Tonight另一個世界And there's another heart只有那心灰意冷的心That's fading in與我The light相伴Don't want your picture別與手機上On my cell phone你的照片相看I want you here
  • 英語諺語|1:惡有惡報,善有善報.What goes around comes around
    go around (also go round British English) phrasal verb6 what goes around comesWhat goes around comes around.你不該虐待他們。善有善報,惡有惡報。不是不報,時候未到。
  • 畢業典禮|Here Comes The Wonder
  • 「在這裡」到底是用「here」還是「in here」啊?
    比如說今天要講的單詞here。從小到大,對here的印象就是:在這裡就是here,不需要加其他介詞。比如說come here來這裡,或者是說I live here我住在這裡。但是在我看美劇的過程中,我發現並非如此。因為我經常會聽到美劇中用到in here這樣的表達。不知道有沒有小夥伴注意到?
  • 歸根結底(when it comes down to it)
    We use the phrase 「when it comes down to it」 to express a general statement or conclusion about something.
  • When It Comes To Us-Frances #節奏·傷感#
    to us當涉及到我們的時候Ooh ooh ooh oh oh oh喔~~~~~When it comes (to us)當涉及到我們的時候Ah Ah Ah Ahahi啊~~~~~When it comes to us當涉及到我們的時候Ooh ooh ooh oh oh oh喔~~~~~When it
  • Here It Is,太好聽了!
    Here is your cart, 這是你的馬車And your cardboard and piss; 還有車上的紙板和穢物;And here is your love 這是你的愛For all of this. 包容了這一切.
  • [英文兒歌] 當樂隊經過時 | When The Band Comes Marching In
    When the drum comes marching in.當鼓聲響起的時候。Oh, when the tuba comes marching in.哦,當大號響起的時候。Oh, when the tuba comes marching in.哦,當大號響起的時候。
  • 【音樂】Come Here Boy
    It's dark in hereAnd visions are flashing into my head as I reminisce
  • 當提到疫情防護,口罩是最有效的 When it comes to......
    When it comes to… When it comes to sth. When it comes to golf, Tiger Woods is the best. That’s true. Tiger Woods is the best.
  • BBC地道英語|It comes with the territory 在所難免,理所當然
    You know if you were famous, you』d get lots of public attention – it comes with the territory.尼爾,醒醒。我覺得他在做白日夢。不要得意忘形。你知道的,如果你很有名,你是會得到許多公眾關注的,這是理所當然的。
  • 《Vrhere》-天津young blood -Vrhere的首張團隊mixtape
    封面由GaRain擔任製作      這是一個來自天津的年輕團隊-Vrhere,我問他們團隊的  L3J-20 「
  • here的同音詞
    here的同音詞是hear,中文釋義為聽到,聽;聽見,音標:英[hr];美[hr]。例句:People flocked to hear him speak.人們成群結隊地去聽他演講。here的同音詞:hear 英 [hr] 美 [hr] vt. 聽到,聽;聽說;審理vi.
  • 歌曲|right here-keshi
    ever in need但如果你需要我And God has you down on your knees生活令你無助And you do not know who to be卻無人可以讓你依靠Then go on and come home to me就回到我身邊吧Because I'm here