analysis.png
背景
最近很多企业都在做机器人(bot),像微软的小冰,微软的小度,包括我最近在用的小米的小爱,一方面是在趁热度提升自己公司的知名度,另一方面就是在收集用户的数据,提升模型的准确性。但是bot在金融领域的应用很少,更不用说在股市和虚拟货币市场领域。
我的想法是这样的,对于虚拟货币市场的货币走势可以进行分析,当然这个分析可能意义不大,但是如果有很多小的点汇集在了一起,那整体的作用就不容忽视了。
工具
code for token 情况分析跟踪
基于python3
用flask暴露restful接口
用beautifulsoup 作为html解析器
接口一共有三个参数
address -> 是以太坊合约地址
page -> 以太坊某个合约地址的排名页码(固定每页显示50)
topN -> topN的持有量 ,最大为50
主题流程
import requestsfrom bs4 import BeautifulSoupfrom flask import Flaskfrom flask import requestclass TokenHolder: def __init__(self,rank,address,balance,percentages): self.rank = rank self.address = address self.balance = balance self.prcentages = percentages def __str__(self): return str(self.__dict__) def getSumPercentagesByAddressAndPage(contract_address,page,topN): url_prefix = "https://etherscan.io/token/generic-tokenholders2?a=" url_suffix = "&s=1E%2b28&p=" request_url = url_prefix + contract_address + url_suffix + str(page); print(request_url) page = requests.get(request_url) soup = BeautifulSoup(page.content, 'html.parser') return getTopNSumPercentage(soup,topN)
解析页面细节处理
def getTopNSumPercentage(soup,n):
total = 0.0
index = 1
if n > 50:
n = 50
if n < 0:
n = 50
for row in soup.find_all('tr')[1:]: if index > n: break
percentages = str(row.find_all("td")[3].text)
b = mapToFloat(percentages)
total = total + b
index = index + 1
return str(total*10) + "%"def getRankNDetail(n):
total = 0.0
index = 1
if n > 50:
n = 50
if n < 0:
n = 50
row = soup.find_all('tr')[n] return getRowDetail(row)def getRowDetail(row):
rowInfo = row.find_all("td")
rank = rowInfo[0].text
address = rowInfo[1].text
balance = rowInfo[2].text
percentages = rowInfo[3].text return TokenHolder(rank,address,balance,formatPercentages(percentages)) for td in row.find_all("td"):
print(td.text)def formatPercentages(n):
new = float(n.replace("%","")) return str(new*10)+"%";def mapToFloat(n):
return float(n.replace("%",""))暴露接口
app = Flask(__name__)@app.route('/')def index():
page = request.args.get('page', default = 1, type = int)
address = request.args.get('address', default = '0xa4e8c3ec456107ea67d3075bf9e3df3a75823db0', type = str)
topN = request.args.get('topN', default = 50, type = int) return getSumPercentagesByAddressAndPage(address,page,topN);if __name__ == '__main__':
app.run(host='127.0.0.1',port=3366)可以看到Top50持有量达到了百分之82,可以实时观察这个数值来进行分析
result.png
作者:freelands
链接:https://www.jianshu.com/p/99f1c53e9475