很早之前发到知乎上边的!!!感兴趣的可以看看
网上找了很多关于买家秀sign值的生产、找到的是Python版本的!
由于本人擅长的PHP语言,这必须得翻译成PHP版本的呀!!
那就来吧、用到的Laravel8框架、类库: HTTP客户端库( guzzlehttp/guzzle )
一、安装HTTP客户端库:
composer require guzzlehttp/guzzle
二、分析请求参数、接口:
接口地址:https://acs.m.taobao.com/h5/mtop.taobao.social.feed.aggregate/1.0/ appKey: 12574478 t: 1560094920983 sign: 9fd51773ab6c80205f4a0c2f97ca14c6 api: mtop.taobao.social.feed.aggregate v: 1.0 timeout: 300000 timer: 300000 type: jsonp dataType: jsonp callback: mtopjsonp1 data: {"params":"{\"nodeId\":\"\",\"sellerId\":\"50852803\"}","cursor":"1","pageNum":"1","pageId":5703,"env":"1"}
其中 t (就是一个时间戳而已) 和 sign是动态生成的。其他都为固定字段!
t 我封装成一个方法,直接调用即可:
/** * @copyright 获取时间戳 * @Author 不问归期_:2019-12-16 * @param boolean $digits [description] * @return [type] [description] */ public function getTimestamp($digits = false) { $digits = $digits > 10 ? $digits : 10; $digits = $digits - 10; if ((!$digits) || ($digits == 10)) { return time(); } else { return number_format(microtime(true), $digits, '', ''); } }
sign 是通过cookies中的参数生成的、直接上代码吧
$url = 'https://acs.m.taobao.com/h5/mtop.taobao.social.feed.aggregate/1.0/?appKey=12574478'; $client = new Client(array( 'cookies' => true )); $response = $client->request('GET', $url); $Cookie = $response->getHeader('Set-Cookie'); $_m_h5_tk = Str::before(Str::after($Cookie[1], '_m_h5_tk='), '_'); $_m_h5_tk_enc = Str::before(Str::after($Cookie[2], '_m_h5_tk_enc='), ';'); $times = $this->getTimestamp(13); $data = '{"params":"{\"sellerId\":\"50852803\",\"pagination\":{\"pageNum\":\"1\",\"pageSize\":\"6\"}}","pageId":5703}'; //$cookie = preg_replace('/\s/', '', '_m_h5_tk=' . $_m_h5_tk . '; _m_h5_tk_enc=' . $_m_h5_tk_enc); $sign = md5($_m_h5_tk . '&' . $times . '&12574478&' . $data);
这两个动态参数都能获取了、接下来就是请求参数拼接 并 请求数据 了!
$url = $url . '&t=' . $times . '&sign=' . $sign . '&data=' . $data; $res = $client->request('GET', $url); $res = $res->getBody()->getContents(); dump($res);
成功!!
三、一些细节处理
我们看data这个参数并分析一下
$data = '{"params":"{\"sellerId\":\"50852803\",\"pagination\":{\"pageNum\":\"1\",\"pageSize\":\"6\"}}","pageId":5703}';
其中sellerId 就是卖家秀id 、pageNum 就是 页数(就是获取第几页的数据) 、pageSize 就不用多说了吧.(这里不说了,不懂的就自行测试吧) data这个参数主要的就是这三个参数了.
优化后的代码:
$sellerId = $request->input('sellerId'); $pageNum = $request->input('pagenum', 1); $pageSize = $request->input('pagesize', 5); $url = 'https://acs.m.taobao.com/h5/mtop.taobao.social.feed.aggregate/1.0/?appKey=12574478'; $client = new Client(array( 'cookies' => true )); $response = $client->request('GET', $url); $Cookie = $response->getHeader('Set-Cookie'); $_m_h5_tk = Str::before(Str::after($Cookie[1], '_m_h5_tk='), '_'); $_m_h5_tk_enc = Str::before(Str::after($Cookie[2], '_m_h5_tk_enc='), ';'); $times = $this->getTimestamp(13); $data = '{"params":"{\"sellerId\":\"' . $sellerId . '\",\"pagination\":{\"pageNum\":\"' . $pageNum . '\",\"pageSize\":\"' . $pageSize . '\"}}","pageId":5703}'; //$cookie = preg_replace('/\s/', '', '_m_h5_tk=' . $_m_h5_tk . '; _m_h5_tk_enc=' . $_m_h5_tk_enc); $sign = md5($_m_h5_tk . '&' . $times . '&12574478&' . $data); $url = $url . '&t=' . $times . '&sign=' . $sign . '&data=' . $data; $res = $client->request('GET', $url); $res = $res->getBody()->getContents(); dump($res);
好了!!!以上就是全部代码、、
Py版的地址:爬取淘宝买家秀,sign值的生成_这个面它又长又宽-CSDN博客
四、最后附上完整代码:
<?php namespace App\Http\Controllers; use GuzzleHttp\Client; use Illuminate\Support\Str; use Illuminate\Http\Request; class TaobaoController { /** * @copyright 淘宝买家秀 * @Author 不问归期_:2019-10-07 * @return [type] [description] */ public function index(Request $request) { $sellerId = $request->input('sellerId'); $pageNum = $request->input('pagenum', 1); $pageSize = $request->input('pagesize', 5); $url = 'https://acs.m.taobao.com/h5/mtop.taobao.social.feed.aggregate/1.0/?appKey=12574478'; $client = new Client(array( 'cookies' => true )); $response = $client->request('GET', $url); $Cookie = $response->getHeader('Set-Cookie'); $_m_h5_tk = Str::before(Str::after($Cookie[1], '_m_h5_tk='), '_'); $_m_h5_tk_enc = Str::before(Str::after($Cookie[2], '_m_h5_tk_enc='), ';'); $times = $this->getTimestamp(13); $data = '{"params":"{\"sellerId\":\"' . $sellerId . '\",\"pagination\":{\"pageNum\":\"' . $pageNum . '\",\"pageSize\":\"' . $pageSize . '\"}}","pageId":5703}'; //$cookie = preg_replace('/\s/', '', '_m_h5_tk=' . $_m_h5_tk . '; _m_h5_tk_enc=' . $_m_h5_tk_enc); $sign = md5($_m_h5_tk . '&' . $times . '&12574478&' . $data); $url = $url . '&t=' . $times . '&sign=' . $sign . '&data=' . $data; $res = $client->request('GET', $url); $res = $res->getBody()->getContents(); dump($res); } /** * @copyright 获取时间戳 * @Author 不问归期_:2019-12-16 * @param boolean $digits [description] * @return [type] [description] */ public function getTimestamp($digits = false) { $digits = $digits > 10 ? $digits : 10; $digits = $digits - 10; if ((!$digits) || ($digits == 10)) { return time(); } else { return number_format(microtime(true), $digits, '', ''); } } }