11-13 4 views
通过“class ETHCrawler”获取到eth实时交易信息:市值,价格,总量,交易量,挖矿难度,主网拥堵情况;
通过“class ETHHolderCrawler”获取eth钱包的实时资金量。
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
import urllib.request import ssl from bs4 import BeautifulSoup from datetime import datetime import re import copy import logging logger = logging.getLogger(__name__) class ETHCrawler: 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 self.url = 'https://etherscan.io' def get_page(self): try: request = urllib.request.Request(self.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 @staticmethod def get_page_items(page_code): if not page_code: logger.error("页面加载失败...") return None soup = BeautifulSoup(page_code, 'html.parser') spans_all = soup.findAll('span') h4s_all = soup.findAll('h4') return spans_all, h4s_all class ETHHolderCrawler: 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 self.url = 'https://etherscan.io/accounts/' + str(self.pageIndex) + '?ps=100' def get_page(self): try: request = urllib.request.Request(self.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
1 2 3 4 5 6 7 8 9 10 11 |
eth_stats['marketcap'] = MARKETCAP eth_stats['blockheight'] = BlockHeight eth_stats['avg_block_time_of_5000_blocks'] = avg_block_time_of_5000_blocks eth_stats['Avg_Hash_Rate_of_the_last_12_Hours'] = Avg_Hash_Rate_of_the_last_12_Hours eth_stats['Average_Difficulty'] = Average_Difficulty eth_stats['prices_usd'] = prices_usd eth_stats['prices_btc'] = prices_btc eth_stats['prices_change'] = prices_change eth_stats['txnc_per_5m'] = txnc_per_5m eth_stats['txnc_per_s'] = txnc_per_s eth_stats["@timestamp"] = datetime.now().isoformat() |
返回结果2
1 2 3 4 5 6 7 8 |
tds = holder_crawler.get_page_items(page_code, i) holder_list['Rank'] = int(tds[0].get_text().strip()) holder_list['Address'] = tds[1].get_text().strip() quantity = re.findall(pattern1, str(tds[2])) holder_list['Balance'] = float(quantity[0].strip().replace(',', '').replace('<b>.</b>', '.')) holder_list['Percentage'] = float(tds[3].get_text().replace('%', '')) holder_list['TxCount'] = int(tds[4].get_text().replace('%', '')) holder_list['@timestamp'] = datetime.now().isoformat() |
如果想赏钱,可以用微信扫描下面的二维码,一来能刺激我写博客的欲望,二来好维护云主机的费用; 另外再次标注博客原地址 itnotebooks.com 感谢!