微软小冰 微博私信api 基于python

说明

需要  python3+ BeautifulSoup flask 目前暂时只支持文字

登陆微博 并将headers保存在根目录headers.txt中
确保已经领养小冰 这里我提供了一个测试账号

详情https://github.com/yanwii/msxiaoiceapi

使用

py xiaoiceapi.py

http://127.0.0.1:5000/?que=你是谁

return:

{  

"status": "sucess",

"text": "你都知道你还问[不屑脸],哈哈党"

}

 

代码:

import requests
import json
import time
from bs4 import BeautifulSoup
from flask import Flask,request,jsonify


class xiaoiceApi():
    
    def __init__(self):
        self.headers = {}
        self.loadheaders()

    def loadheaders(self):
        '''
            导入headers
        '''
        with open("./headers.txt") as headers:
            line = headers.readline().strip()
            while line:
                key = line.split(":")[0]
                self.headers[key] = line[len(key)+1:]
                line = headers.readline().strip()            

    def chat(self, input_strs):
        if not self.headers:
            return self.dicts("error", "请打开浏览器 复制并将headers放入headers.txt中")
        '''
        聊天
        
            args (str):   
                input_strs  问题  
            return (dict):  
                status      状态  
                text        内容        
        '''
        data = {
            'location':'msgdialog',
            'module':'msgissue',
            'style_id':1,
            'text':input_strs,
            'uid':5175429989,
            'tovfids':'',
            'fids':'',
            'el':'[object HTMLDivElement]',
            '_t':0,
        }
        
        try:
            url = 'http://weibo.com/aj/message/add?ajwvr=6'
            page = requests.post(url, data=data, headers=self.headers)
            self.savePage(page.text, "./tmp/postpage.txt")
            if page.json()['code'] == '100000':
                text = self.loop(input_strs)
                return self.dicts("succeed", text)
            else:
                return self.dicts("failed", page.json()['msg'])
        except Exception as e:
            return self.dicts("error", e)
    
    def dicts(self, status, text):
        '''
            包装return
        '''
        return {"status":status, "text":text}

    def loop(self, input_strs):
        '''  
            刷新直到获取到回答
        '''
        times = 1
        while times:
            times += 1
            response = requests.get("http://weibo.com/aj/message/getbyid?ajwvr=6&uid=5175429989&count=1&_t=0" , headers=self.headers)
            self.savePage(response.text, "./tmp/response.txt")
            soup = BeautifulSoup(response.json()['data']['html'], "lxml")
            text = soup.find("p", class_='page').text
            if text != input_strs or times > 20:
                break
            time.sleep(1)
        return text
            
    def savePage(self, text, file):
        with open(file, "w") as f:
            f.write(text)
    
    def api(self):
        app = Flask(__name__)

        @app.route("/")
        def index():
            que = request.args.get("que")
            ans = self.chat(que)
            return jsonify(ans)
        app.run()

xb = xiaoiceApi()
xb.api()
    

转载于:https://my.oschina.net/u/3500085/blog/907492

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值