我正在尝试使用以下代码将'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>
答案 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))