如果你有想要特別關(guān)注的微博賬號(hào)旋奢,但是又沒有裝手機(jī)微博(或者說關(guān)閉了微博的垃圾消息提醒)又不是很喜歡刷微博的人刻像,想要第一時(shí)間獲取關(guān)心的微博消息怎么辦呢。
或許你可以試試這樣鞠值,利用爬蟲每隔一小段時(shí)間間隔就重新獲取一次關(guān)注賬號(hào)的微博年枕,比對(duì)是否有新消息芳悲,如果有則發(fā)送到你的微信賬號(hào)上贮喧,當(dāng)然也可以發(fā)送到你的郵箱栅盲,不過微信更及時(shí)更友好點(diǎn)。
這個(gè)想法最早在一個(gè)python課程(很抱歉記不得是哪個(gè)課程了白胀,記憶力太差椭赋。。)里看到或杠,這里簡單實(shí)現(xiàn)下哪怔。
爬蟲部分利用requests庫和BeautifulSoup庫很容易完成爬取和解析。實(shí)現(xiàn)時(shí)間間隔就用最簡單的睡眠(time.sleep
)向抢。發(fā)送到微信使用itchat庫發(fā)送到自己的文件助手就好了认境,很簡單。
當(dāng)然電腦端同一微信賬號(hào)登錄只能有一個(gè)挟鸠,也就是不能同時(shí)使用該賬號(hào)的電腦版微信了叉信,解決方案是采用登錄微信小號(hào)發(fā)送給大號(hào)的形式,這樣消息提醒也更明確艘希,不容易忽略硼身。
這里打包好的可執(zhí)行程序只是發(fā)送文件助手的方案。
鏈接:http://pan.baidu.com/s/1kVvCpvh 密碼:cxza
代碼github
效果圖枢冤,以segmentfault微博賬號(hào)為例
使用方法
輸入你的cookie和要獲取的微博賬號(hào)uid
在對(duì)彈出的二維碼微信掃碼登錄后就可以接收到消息了
獲取你的微博賬戶cookie
獲取用戶uid
找到用戶的uid 如 segmentfault 為 2036070420
代碼
# 使用pyinstaller 一鍵生成可執(zhí)行文件 位于dist目錄下
# pyinstaller weibohelper.py --onefile
from bs4 import BeautifulSoup
import time
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
print('請(qǐng)輸入cookie:')
cookie = input()
print('請(qǐng)輸入uid:')
uid = input()
uids = (uid, )
headers = {
"Cookies": cookie,
"User-Agent":'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1'
}
r = requests.session()
r.headers.update(headers)
# 獲取某條微博的評(píng)論
def get_comments_from_one_weibo(id):
try:
json = r.get(
('https://m.weibo.cn/api/comments/show?'
'id={0}&page=1').format(id),
verify=False,
).json()
if json['ok'] == 1:
comments = []
for c in json['data']:
comments.append(BeautifulSoup(c['text'], 'lxml').text)
comments.append(c['user']['screen_name'])
comments.reverse()
return comments
else:
return ()
except:
return ()
# 根據(jù)用戶uid獲取該用戶第一頁的微博消息
def get_single_user_fisrt_weibo(uid):
page_num = 1
nickname = None
weibo = None
try:
json = r.get(
('https://m.weibo.cn/api/container/getIndex?'
'is_search[]=0&'
'visible[]=0&'
'is_all[]=1&'
'is_tag[]=0&'
'profile_ftype[]=1&'
'page={0}&'
'jumpfrom=weibocom&'
'sudaref=weibo.com&'
'type=uid&'
'value={1}&'
'containerid=107603{1}').format(page_num, uid),
verify=False,
).json()
except:
return None, None
if json['ok'] == 0:
print('sth wrong')
return None, None
else:
for card in json['cards']:
if card['card_type'] == 9:
weibo = [
card['mblog']['created_at'],
BeautifulSoup(
card['mblog']['text'], 'lxml'
).text.replace(' \u200b\u200b\u200b', ''),
*get_comments_from_one_weibo(
card['mblog']['id']),
]
nickname = card['mblog']['user']['screen_name'] + ' '
break # 取第一個(gè)即可
print('success for', nickname, ' - time', time.ctime())
return nickname, weibo
# 使用itchat登錄網(wǎng)頁版微信
import itchat
itchat.auto_login()
# 以小號(hào)登錄形式鸠姨,要先找到大號(hào)的賬戶
# username = itchat.search_friends(name='xx')[0]['UserName']
username = 'filehelper'
import json
from collections import defaultdict
records = defaultdict(lambda : (None, None))
while 1:
for uid in uids:
nickname, weibo = get_single_user_fisrt_weibo(uid)
try_times = 5
while nickname == None:
nickname, weibo = get_single_user_fisrt_weibo(uid)
try_times -= 1
if try_times == 0:
break
if try_times == 0:
continue
if records[nickname][1:] != weibo[1:]:
# print('發(fā)現(xiàn)新微博:', weibo)
itchat.send(
'\n---\n'.join((nickname, *weibo)),
toUserName=username)
records[nickname] = weibo # 記錄當(dāng)前微博信息
with open('data.json', 'w', encoding='utf-8') as f:
json.dump(records, f, ensure_ascii=False)
else:
print('沒有新消息', time.ctime())
pass
time.sleep(120)