微博私信协议
2021-07-16 本文已影响0人
后山小鲨鱼
用的是公开的协议
如有侵权,联系删除。
进入到某个用户的私信页面,发送私信。
可以发现,是个post请求
主要关注data里面的数据
text :要发送的内容
uid:要发送私信的用户id
clientid:一个加密的参数,服务端返回,或js生成
source:本人的id,但和用户主页的id不同,还不知道从哪里来。
刷新一下页面,重新捕获一下链接,看是否能找到clientid
在某个链接里面发现了他
这条链接是post请求
https://web.im.weibo.com/im/handshake
data:[{"id":"1","version":"1.0","minimumVersion":"1.0","channel":"/meta/handshake","supportedConnectionTypes":["long-polling","callback-polling"],"advice":{"timeout":60000,"interval":0}}]
data没有特殊的值,固定就好。
clientid的话,也可以在某个js文件中找到
https://conchfairy.sinajs.cn/chat/js/app.000f4c48.js
这样的话,参数就找齐了。
需要注意一点的是各个链接content-type的类型。
import requests
from urllib.parse import quote
import json
import re
import time
class wp():
def __init__(self):
self.cookie = '你的cookie'
self.get_clientid()
self.getsoureid()
# 得到sourceid
def getsoureid(self):
url = 'https://conchfairy.sinajs.cn/chat/js/app.000f4c48.js'
headers = {
'Cookie':self.cookie,
}
rg = requests.get(url,headers = headers)
rt = rg.text
sourceid = re.findall(r'source=(\d+)"',rt)[-1]
self.sourceid = sourceid
# 得到clientid
def get_clientid(self):
url = 'https://web.im.weibo.com/im/handshake'
headers = {
'Content-Type': 'application/json;charset=UTF-8',
'Cookie': self.cookie,
'Referer': 'https://api.weibo.com/'
}
data = [{"id":"1","version":"1.0","minimumVersion":"1.0","channel":"/meta/handshake","supportedConnectionTypes":["long-polling","callback-polling"],"advice":{"timeout":60000,"interval":0}}]
r = requests.post(url,headers = headers,data = json.dumps(data),verify = False)
if r.status_code == 200:
rj = r.json()
print(rj)
self.clientid = rj[0]['clientId']
# 私信
def sixin(self,uid,content):
source = self.sourceid
content = quote(content)
url = 'https://api.weibo.com/webim/2/direct_messages/new.json'
data = f'text={content}&uid={uid}&extensions=%7B%22clientid%22%3A%22{self.clientid}%22%7D&is_encoded=0&decodetime=1&source={source}'
print(data)
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Cookie': self.cookie,
'Referer': 'https://api.weibo.com/chat/'
}
rp = requests.post(url,headers = headers,data = data)
print(rp.status_code)
if rp.status_code == 200:
print(rp.text)
仅用于自己的账号,请不要批量操作,非法操作。