Python的新手,我试图使用BeautifulSoup从 etherscan.com 网页中提取"ETH余额",代码如下:
import bs4, requests
res = requests.get('https://etherscan.io/address/0x93673eeed88fda9423b8037374164383df54aec1')
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text, 'html.parser')
ethBal = soup.find("td", text="ETH Balance").find_next("td").text
print('The ETH blance is '+ ethBal)
但是,我不断收到以下错误:
Traceback (most recent call last):
File "/Users/tfountain/Desktop/python_work/c2.py", line 7, in <module>
ethBal = soup.find("td", text="ETH Balance").find_next("td").text
AttributeError: 'NoneType' object has no attribute 'find_next'
我哪里出错了,获得ETH余额的最佳方式是什么?
看看页面源代码,HTML 是:
<td>ETH Balance:
</td>
<td>
0 Ether
</td>
您正在搜索text='ETH Balance'
.但是文本在末尾用换行符ETH Balance:
。
因此,使用此:
eth_bal = soup.find('td', text='ETH Balance:n').find_next('td').text.strip()
print(eth_bal)
# prints '0 Ether'
我使用正则表达式来查找包含"以太"一词的td
,并刚刚解析了该标签。
法典:
import bs4, requests, re
res = requests.get('https://etherscan.io/address/0x93673eeed88fda9423b8037374164383df54aec1')
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text, 'html.parser')
ethBal = soup.find('td', text=re.compile('Ether')).text
print('The ETH blance is '+ ethBal)
输出:
The ETH blance is
0 Ether