环境说明

  • TP6
  • PHP 8.0
  • QueryList 4.2.8

步骤说明

需求

采集www.bscscan.com中交易信息

安装 QueryList

composer require jaeger/querylist

代码示例

class Bsc
{

private function getSid($html)
{
$html = strstr($html, 'var sid =');
$html = substr($html, strpos($html, 'var sid = \'') + 11);
return substr($html, 0, strpos($html, "'"));
}

private function getPageData($page)
{
$otherArgs = [
'headers' => [
'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
],
];

$tokenUrl = 'https://www.bscscan.com/token/' . $this->contractaddress;
$tokenQl = QueryList::get($tokenUrl, null, $otherArgs);

$sid = $this->getSid($tokenQl->getHtml());

$url = 'https://www.bscscan.com/token/generic-tokentxns2?m=normal&contractAddress=' . $this->contractaddress . '&a=&sid=' . $sid . '&p=' . $page;
$ql = QueryList::get($url, null, $otherArgs);

// 采集交易总条数
$total = $ql->find('.mb-4 p')->text();

// 采集表格
$table = $ql->find('table');
// 采集表头
$tableHeader = $table->find('tr:eq(0)')->find('th')->texts();
// 采集表的每行内容
$tableRows = $table->find('tr:gt(0)')->map(function ($row) {
return $row->find('td')->texts()->all();
});

// 采集表的每行Method
$methodsObject = $table->find('tr:gt(0)')->map(function ($row) {
return $row->find('td .u-label')->attr('title');
});
$methods = $methodsObject->all();

$insertData = $tableRows->all();
foreach ($insertData as $key => &$item) {
$item[] = $methods[$key];
foreach ($item as $itemKey => &$itemVal) {
// 交易时间
if ($itemKey == 2) {
$itemVal = strtotime($itemVal);
}
// 垃圾数据
if ($itemKey == 1 || $itemKey == 3 || $itemKey == 5) unset($item[$itemKey]);
}
}

$res = [];
foreach ($insertData as $items) {
$temp = [
'txn_hash' => $items[0],
'method' => $items[8],
'from' => $items[4],
'to' => $items[6],
'quantity' => $items[7],
'created' => $items[2],
];
$temp['unique_hash'] = hash('sha256', json_encode($temp));
$res[] = $temp;
unset($temp);
}
unset($insertData);

return $res;
}
}