在Python中将''NodeList'项转换为int()?

时间:2017-12-01 01:29:07

标签: python xml variables type-conversion urllib2

我正在尝试使用以下代码将'NodeList'对象转换为int()。

# DEFINE DATA FROM URL
dom = minidom.parse(urllib2.urlopen(url))

# SELECT AMOUNT
amount = dom.getElementsByTagName('amount')

# DEFINE VARIABLE AS INT
amount = int(amount)

当我运行它时,我得到:

  

TypeError:int()参数必须是字符串或数字,而不是'NodeList'

提前感谢您的帮助。

编辑:两个示例节点是:

<DOM Element: amount at 0x10e5c87a0>
<amount currency ="USD">142113</amount>

<DOM Element: amount at 0x10eccfcf8>
<amount currency="USD">140787</amount>

1 个答案:

答案 0 :(得分:0)

您可以使用请求和BeautifulSoup:

import requests
from bs4 import BeautifulSoup

page = requests.get(url)
soup = BeautifulSoup(page.text, 'lxml')
amounts = soup.select('amount') # A list of amount tags    
for amount in amounts:
    print(int(amount.text))