如何在beauthulsoup中获得下一个td值

2024-10-03 11:24:52 发布

您现在位置:Python中文网/ 问答频道 /正文

Python新手,我尝试使用beauthoulsoup从an中获取“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)

但是,我一直得到的错误是:

^{pr2}$

我哪里出错了?怎样才能获得ETH平衡?在


Tags: 代码textimportan网页resfindrequests
2条回答

我使用regex查找包含单词“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)

输出:

^{pr2}$

查看页面源代码,HTML是:

<td>ETH Balance:
</td>
<td>
0 Ether
</td>

您正在搜索text='ETH Balance'。但是文本是ETH Balance:,末尾有一个换行符。在

所以,使用这个:

^{pr2}$

相关问题 更多 >