Search code examples
pythonxmlvariablestype-conversionurllib2

Converting ''NodeList' item to int() in Python?


I'm trying to convert a 'NodeList' object to an int() with the following code.

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

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

# DEFINE VARIABLE AS INT
amount = int(amount)

When I run it, I get:

TypeError: int() argument must be a string or a number, not 'NodeList'

Thanks in advance for the help.

EDIT: Two example Nodes are:

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

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

Solution

  • You can use requests and 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))