将 'NodeList' 项转换为 Python 中的 int()?
Converting ''NodeList' item to int() in Python?
我正在尝试使用以下代码将 '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() argument must be a string or a number, not 'NodeList'
在此先感谢您的帮助。
编辑:两个示例节点是:
<DOM Element: amount at 0x10e5c87a0>
<amount currency ="USD">142113</amount>
<DOM Element: amount at 0x10eccfcf8>
<amount currency="USD">140787</amount>
您可以使用请求和 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))
我正在尝试使用以下代码将 '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() argument must be a string or a number, not 'NodeList'
在此先感谢您的帮助。
编辑:两个示例节点是:
<DOM Element: amount at 0x10e5c87a0>
<amount currency ="USD">142113</amount>
<DOM Element: amount at 0x10eccfcf8>
<amount currency="USD">140787</amount>
您可以使用请求和 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))