每當我看到這一幅幅漫畫就想起了我那逝去的青春,作爲一隻快樂的小狗在夕陽下奔跑,追逐那位(些)不曾回頭的她(們),如果早早學會python,成爲一隻“雲舔狗”,也許她(們)就能回頭了
1.“雲舔狗” - 自動化微信腳本(tiangou)
正如上一篇說到我們可以用python來分析我們的好友,那我們是不是可以用python來聯繫我們的好朋友,這樣用python來解放我們的雙手,同時跟多個朋友聯繫,成爲一隻“雲舔狗”豈不是指日可待,想想就很美好
下面展示代碼運行結果和心路歷程,
源碼我放到#公衆號【一行數據】上,大家關注即可查閱
閱讀原文就能看到了哦
2.每日發送早安,晚安,
首先我們從發送早安和晚安做起,實現定時定點給她(們)發送微信,當然女神朋友多的話,建一個列表也可以寫個for循環也沒什麼問題。 下面關門放代碼看看結果
# 登錄微信
itchat.auto_login(hotReload=False)
# 獲取朋有列表
friends_list = itchat.get_friends(update=True)
name = itchat.search_friends(name=u'阿櫻')
Aying = name[0]["UserName"]
#獲取時間
while True:
now= datetime.datetime.now()
if now.hour == 6 and now.minute == 00:
itchat.send('早安',Aying)
elif now.hour == 22 and now.minute == 00:
itchat.send('晚安',Aying)
time.sleep(30)
測試結果還是可以的,再加上時間判斷和while循環,十行代碼實現一年365天發送早安和晚安。如果早安設置的6點時間感覺太晚,還可以設置五點半,設置五點,設置四點半,總有一個時間能溫馨的提醒她(們)起牀。這裏也只發送了一次,寫個循環就可以發送多次了,不過需要停頓幾秒的發送,太頻繁的話可能會被封,不過被封了換個微信號就好啦,有志者事竟成
2.多元素定時發送
2.1 發送情話
如果覺得早安和晚安太單調的話可以加入一些好聽的語句(名人名言),我去某乎蒐羅了一些,大家如果有時間去名人名言網站,鬼故事網站每日定時爬取,然後伴着早安和晚安發送過去,給她(們)帶去一次次驚喜有木有,事半功倍的感覺有木有,好感度蹭蹭往上漲
while True:
now= datetime.datetime.now()
if now.hour == 6 and now.minute == 00:
itchat.send('早安',Aying)
itchat.send(yulu.qinghua[random.randint(0,50)],Aying)
elif now.hour == 22 and now.minute == 00:
itchat.send('晚安',Aying)
itchat.send(yulu.qinghua[random.randint(0,50)],Aying)
time.sleep(30)
2.2 發送傳說中的舔狗語句
當然了我也搜一集了一些"舔狗"語錄,句句扎心,想用代碼發送這些語錄同上,各位讀者大老爺們看看就好啦
2.3 發送天氣預報
魯迅曾說過:一葉知春秋,一句知冷暖。 靈感來了,我還可以實時播報天氣預報呀,給朋友全天候的關懷,讓她(們)知道無時無刻都關心她(們)冷暖的人到底是誰
首先利用爬蟲技術將“http://t.weather.sojson.com/api/weather/city/”所有城市數字代碼爬取下來,然後拼接天氣網站網址請求訪問,然後實時獲得當地天氣情況,通過python實時播報, 下面關門放代碼看看結果
def main(city_name):
#api地址
url = 'http://t.weather.sojson.com/api/weather/city/'
#通過城市的中文獲取城市代碼
city_code = city.citycode[city_name]
#網絡請求,傳入請求api+城市代碼
response = requests.get(url + city_code)
#將數據以json形式返回,這個d就是返回的json數據
d = response.json()
dd = "城市: "+ d["cityInfo"]["parent"]+d["cityInfo"]["city"] + \
"\n時間: "+ d["time"]+ d["data"]["forecast"][0]["week"] + \
"\n溫度: "+ d["data"]["forecast"][0]["high"]+ d["data"]["forecast"][0]["low"] + \
"\n天氣: "+ d["data"]["forecast"][0]["type"] + \
"\n注意: "+ d["data"]["forecast"][0]["notice"]
print(dd)
3.和女神自動聊天
在"雲舔狗"的不懈努力下,終於得到女神(們)的回覆了,一時間回覆不過來,那麼自動回覆助手顯得非常重要了
3.1 弱-人工智能版本
現在nlp(自然語言處理)領域非常的火熱,建議大家有時間都去學學,現在市場上薪資18-25k(不分大小公司),如果僅僅想使用,我們可以調用百度雲,阿里雲等一些平臺提供好的的接口就好了。例如我用的是圖靈機器人的接口 下面關門放代碼
# 抓取網頁
def getHtmlText(url):
try:
r = requests.get(url, timeout=30)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except:
return ""
# 自動回覆
# 封裝好的裝飾器,當接收到的消息是Text,即文字消息
@itchat.msg_register(['Text', 'Map', 'Card', 'Note', 'Sharing', 'Picture'])
def text_reply(msg):
# 當消息不是由自己發出的時候
if not msg['FromUserName'] == Name["一行數據"]:
# 回覆給好友
url = "http://www.tuling123.com/openapi/api?key=d8dee202a87940bd866332938dd9ec52&info="
url = url + msg['Text']
print(url)
html = getHtmlText(url)
message = re.findall(r'\"text\"\:\".*?\"', html)
reply = eval(message[0].split(':')[1])
return reply
if __name__ == '__main__':
itchat.auto_login()
<span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># 獲取自己的UserName</span>
friends = itchat.get_friends(update=<span class="hljs-literal" style="color: #56b6c2; line-height: 26px;">True</span>)[<span class="hljs-number" style="color: #d19a66; line-height: 26px;">0</span>:]
Name = {}
Nic = []
User = []
<span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">for</span> i <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">in</span> range(len(friends)):
Nic.append(friends[i][<span class="hljs-string" style="color: #98c379; line-height: 26px;">"NickName"</span>])
User.append(friends[i][<span class="hljs-string" style="color: #98c379; line-height: 26px;">"UserName"</span>])
<span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">for</span> i <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">in</span> range(len(friends)):
Name[Nic[i]] = User[i]
itchat.run()
3.2 真-人工智能版本
python內心: 我的天吶,你的女神都回你了,你還在用代碼跟她(們)聊天?????
4.未解bug
目前唯一的難題就是如何在她(們)將你拉黑之如何繼續發送消息,當然也想過批量發送添加好友請求,實在不行用python批量發短信,再不濟也可以嘗試一下打電話(例如轟死你)和她(們)用語言交流,如果說讓你別打了那肯定是在考驗你的恆心,我們一定要持之以恆,,,再後來感覺這個冬天還是看守所的暖氣比較足,就是沒網。。。。。
參考:
【1】https://www.zhihu.com/question/308085320/answer/627859401
【2】https://www.jb51.net/article/136023.htm