11-13 4 views
通过“class EOSHolderCrawler”获取eos钱包的实时资金量。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import urllib.request import ssl from bs4 import BeautifulSoup from datetime import datetime import re import copy import logging logger = logging.getLogger(__name__) class EOSHolderCrawler: def __init__(self): self.pageIndex = 1 self.user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' self.context = ssl._create_unverified_context() self.headers = {'User-Agent': self.user_agent} self.stories = [] self.enable = False def get_page(self): try: url = 'https://etherscan.io/token/tokenholderchart/0x86fa049857e0209aa7d9e616f7eb3b3b78ecfdb0' request = urllib.request.Request(url, headers=self.headers) response = urllib.request.urlopen(request, context=self.context) page = response.read().decode('utf-8', 'ignore') return page except urllib.request.URLError as e: if hasattr(e, "reason"): logger.error(u"连接Etherscan失败,错误原因", e.reason) return None def get_page_items(self, page_code, trs_index): if not page_code: logger.error("页面加载失败...") return None soup = BeautifulSoup(page_code, 'html.parser') trs = soup.findAll('tr') tds = trs[trs_index].find_all('td') return tds |
返回结果
1 2 3 4 5 6 |
tds = holder_crawler.get_page_items(page_code, j) holder_list['rank'] = int(tds[0].get_text().strip()) holder_list['address'] = tds[1].get_text().strip() holder_list['quantity'] = float(tds[2].get_text()) holder_list['percentage'] = float(tds[3].get_text().replace('%', '')) holder_list['@timestamp'] = datetime.now().isoformat() |
如果想赏钱,可以用微信扫描下面的二维码,一来能刺激我写博客的欲望,二来好维护云主机的费用; 另外再次标注博客原地址 itnotebooks.com 感谢!