我正在尝试从XML页面中找到产品的最新版本。在大多数情况下,xml中的元素“ component-version”是版本号,但在一种情况下,它是字符串“ Current Version”,当我将LooseVersion与其他版本(例如1.12)进行比较时,会出现错误。 1,1.11.0)。我想知道是否有解决此错误的方法。提前致谢!
import requests
import xml.etree.ElementTree as ET
from distutils.version import LooseVersion
product = 'Product Name'
def get_latest_version(product):
r = requests.get("http://samplexmlwebsite.com")
variable = r.content.decode("utf-8") #Turned from a byte type to a variable
root = ET.fromstring(variable)
version_number =""
for item in root.findall('item'):
name = item.find('component-name').text
version = item.find('component-version').text
if name == product:
if not version_number:
version_number = version
elif LooseVersion(version_number) < LooseVersion(version):
version_number = version
print(version_number)
return version_number
get_latest_version(product)