如何在BeautifulSoup中获取下一个td值

时间:2018-03-01 00:23:50

标签: python beautifulsoup

Python的新手,我正在尝试使用BeautifulSoup来提升" ETH余额"来自etherscan.com网页的代码:

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余额的最佳途径是什么?

2 个答案:

答案 0 :(得分:0)

我使用正则表达式查找包含“&#39;以太”字样的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

答案 1 :(得分:0)

看一下页面源代码,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'