Code Monkey home page Code Monkey logo

weibot's Introduction

weibot

微博机器人

安装

composer require hanson/weibot:dev-master -vvv

文档

登录

<?php

include_once __DIR__.'/../vendor/autoload.php';

$weibo = new \Hanson\Weibot\Weibot([
    'username' => '',
    'password' => '',
    'cookie_path' => __DIR__.'/cookie', // cookie 存储路径
    'debug' => []
]);

搜索 Search

<?php
$search = $weibo->search;

$search->getData([
    'keyword' => '关键词',
//     'page' => 1, // 页数
//    'start_at' => '2019-11-07-6', # yyyy-mm-dd-h 时间筛选
//    'end_at' => '2019-12-07-8',
]);

我的关注 Follow

<?php
$follow = $weibo->follow;

// 获取我的关注
$follow->getData([
//     'page' => 1, // 页数
]);

// 取消关注 
$follow->unfollow($uid);

微博 Post

<?php
$post = $weibo->post;

/**
 * 发送微博
 *
 * @param string $text 发送的微博内容
 * @return mixed
 */
$post->send($text);

/**
 * 评论微博
 *
 * @param $mid string 微博 id
 * @param $content string 评论内容
 * @param bool $forward 是否转发
 * @return mixed
 */
$post->comment($mid, $content, $forward = false)

/**
 * 转发微博
 *
 * @param $mid
 * @param $reason string 转发内容
 * @param bool $isComment 是否评论
 * @param bool $isCommentBase 是否对原微博评论
 * @return mixed
 */
$post->forward($mid, $reason, $isComment = false, $isCommentBase = false)

/**
 * 删除微博
 * 
 * @param $mid string 微博id
 * @return false|string
 */
$post->delete($mid)

// 我的微博(待开发)
$post->getData()

参与开发更多 API

基于 weibot,开发微博的抓包工作会更加简单

一、登录

微博很多操作都需要登录,所以写脚本的时候先登录,让 cookie 存储起来 $weibo->login()

二、抓包

根据浏览器看到的请求,我们可以尝试模拟一下

<?php
// $client 已经是一个带 cookie 的“浏览器”客户端了,根据实际情况进行 get 或者 post
$client = \Hanson\Weibot\Api\Api::getClient();

$response = $client->post('http://weibo.com', [
    'header' => [
        // 如果有特殊 header 需求    
    ],
    'form_params' => [
        // 各种请求参数
    ]
]);

// 得到的 response 有可能是页面,也有可能是接口,自行处理
$data = json_decode($response->getBody()->getContents(), true);

相关头绪

  • 微博部分页面是基于页面渲染的模式
  • 微博的渲染并不按套路出牌,而是使用了 FM.view 的内部框架
  • 抓取内容需要先在 script 里正则匹配出来相关的 HTML。 例如:preg_match_all('/Pl_Official_MyProfileFeed__20.*html\":\"(.*)\"}/', $html, $matches);
  • 有部分地方是异步接口的,例如下滑滚动分页

weibot's People

Contributors

dependabot[bot] avatar diaolizhi avatar hanson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

weibot's Issues

直接运行demo报错了(cURL error 3: <url> malformed)

下面是报错:

$ /usr/bin/php7.1 login.php
PHP Notice:  Undefined index: crossDomainUrlList in /home/yxz/samba_share/project/weibot_test/vendor/hanson/weibot/src/Auth/Auth.php on line 80
PHP Stack trace:
PHP   1. {main}() /home/yxz/samba_share/project/weibot_test/login.php:0
PHP   2. Hanson\Weibot\Weibot->login() /home/yxz/samba_share/project/weibot_test/login.php:12
PHP   3. Hanson\Weibot\Auth\Auth->login() /home/yxz/samba_share/project/weibot_test/vendor/hanson/weibot/src/Weibot.php:35
PHP   4. Hanson\Weibot\Auth\Auth->sinaLogin() /home/yxz/samba_share/project/weibot_test/vendor/hanson/weibot/src/Auth/Auth.php:29
PHP Fatal error:  Uncaught GuzzleHttp\Exception\RequestException: cURL error 3: <url> malformed (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) in /home/yxz/samba_share/project/weibot_test/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php:201
Stack trace:
#0 /home/yxz/samba_share/project/weibot_test/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php(155): GuzzleHttp\Handler\CurlFactory::createRejection(Object(GuzzleHttp\Handler\EasyHandle), Array)
#1 /home/yxz/samba_share/project/weibot_test/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php(105): GuzzleHttp\Handler\CurlFactory::finishError(Object(GuzzleHttp\Handler\CurlHandler), Object(GuzzleHttp\Handler\EasyHandle), Object(GuzzleHttp\Handler\CurlFactory))
#2 /home/yxz/samba_share/project/weibot_test/vendor/guzzlehttp/guzzle/src/Handler/CurlHandler.php(43): GuzzleHttp\Handler\CurlFactory::finish(Object(GuzzleHttp\Handler\CurlHandler), Object(GuzzleHttp\Handler\EasyHandle), Object(GuzzleHttp\Handler\CurlFactory))
#3 /home/yxz/samba_share/project/weibot_test/ven in /home/yxz/samba_share/project/weibot_test/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php on line 201

这是我运行的源码:

<?php

use Hanson\Weibot\Api\Api;

include_once __DIR__.'/vendor/autoload.php';
$weibo = new \Hanson\Weibot\Weibot([
    'username' => 'imzhi',
    'password' => 'anzhan0511',
    'cookie_path' => __DIR__.'/cookie',
    'debug' => []
]);
$follow = $weibo->login()->follow;

// $follow = $weibo->follow;

// $follow->getData([
//     'page' => 1, // 页数
// ]);
// var_dump($follow);

关键异常是:PHP Fatal error: Uncaught GuzzleHttp\Exception\RequestException: cURL error 3: <url> malformed,请看下啥原因?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.