Tom*_*ain -1 python beautifulsoup
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)
Run Code Online (Sandbox Code Playgroud)
但是我不断收到错误消息:
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'
Run Code Online (Sandbox Code Playgroud)
我哪里出错了,获得 ETH 余额的最佳方式是什么?
查看页面源码,HTML为:
<td>ETH Balance:
</td>
<td>
0 Ether
</td>
Run Code Online (Sandbox Code Playgroud)
您正在寻找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'
Run Code Online (Sandbox Code Playgroud)